2017-05-31 100 views
0

我在報告中遇到了平均子查詢的一些問題。我試圖獲得count(distinct(d.orderno)),avg(count(distinct(d.orderno)))值,這樣我就可以比較計數和平均值之間的百分比,但它根本不起作用。請看看我的代碼:SSRS中的平均值

SELECT 

d.packingoperator, 
d.packingunit, 
datepart(hh, d.datetimepacked) as hourPacked, 
avg(count(distinct(d.orderno))) as targetrate, 
count(distinct(d.orderno)) as orderspacked, 
    (select count(distinct(d1.orderno)) 
    from mck_hvs.oldorderdetails d1 with (nolock) 
     where d1.refrigerate != 'N' 
     and convert(date, d1.datetimepacked) = convert(date, @date) 
     and d1.packingoperator = d.packingoperator 
    and datepart(hh, d1.datetimepacked) = datepart(hh,d.datetimepacked)) as coldcount 

FROM 

mck_hvs.oldorderdetails d with (nolock) 

WHERE 

convert(date, d.datetimepacked) = convert(date, @date) 

GROUP BY 

d.packingoperator, 
datepart(hh, d.datetimepacked), 
d.packingunit 

ORDER BY 

d.packingoperator, 
datepart(hh, d.datetimepacked) 

我也試過這個選項,以及:

SELECT 

d.packingoperator, 
d.packingunit, 
datepart(hh, d.datetimepacked) as hourPacked, 
count(distinct(d.orderno)) as orderspacked, 
    (select count(distinct(d1.orderno)) 
    from mck_hvs.oldorderdetails d1 with (nolock) 
     where d1.refrigerate != 'N' 
     and convert(date, d1.datetimepacked) = convert(date, @date) 
     and d1.packingoperator = d.packingoperator 
    and datepart(hh, d1.datetimepacked) = datepart(hh,d.datetimepacked)) as coldcount, 
     (select avg(target) from (
     select count(distinct(d2.orderno)) as target 
     from mck_hvs.oldorderdetails d2 with(nolock) 
     where convert(date, d2.datetimepacked) = convert(date, @date) 
     and d2.packingoperator = d.packingoperator 
     and datepart(hh, d2.datetimepacked) = datepart(hh, d.datetimepacked)) as targetrate 


FROM 

mck_hvs.oldorderdetails d with (nolock) 

WHERE 

convert(date, d.datetimepacked) = convert(date, @date) 

GROUP BY 

d.packingoperator, 
datepart(hh, d.datetimepacked), 
d.packingunit 

ORDER BY 

d.packingoperator, 
datepart(hh, d.datetimepacked) 
+2

你剛剛標記了這個每個包含「sql」的標籤嗎? – Uueerdo

+0

看到如何只有四個標籤,我不明白你的關注。這個問題屬於他們的範疇,那麼我爲什麼不呢? @Uueerdo –

+0

MySQL和SQL Server通常可以有非常不同的答案。沒有內置GROUP_CONCAT的MySQL沒有CTE和MSSQL是立即想到的兩件事情。出於某種原因,示例中的樣式讓我想起了MySql,但'nolock'的存在暗示了MSSQL。 – Uueerdo

回答

0

你有沒有想過打破了你的一些子聚合成交叉適用?請看下面。這是非常不雅的代碼,但它可能能夠讓你走上正確的軌道。

SELECT 
    d.packingoperator, 
    d.packingunit, 
    DATEPART(hh, d.datetimepacked) as hourPacked, 
    AVG(ct.orderNoCt) as targetrate, 
    COUNT(dt.orderNoDt) as orderspacked, 
     (
     SELECT 
      COUNT(DISTINCT(d1.orderno)) 
     FROM mck_hvs.oldorderdetails d1 with (NOLOCK) 
      where d1.refrigerate != 'N' 
      and CONVERT(date, d1.datetimepacked) = CONVERT(date, @date) 
      and d1.packingoperator = d.packingoperator 
     and DATEPART(hh, d1.datetimepacked) = DATEPART(hh,d.datetimepacked) 
     ) as coldcount 
FROM 
mck_hvs.oldorderdetails d with (nolock) 
CROSS APPLY 
    (
    SELECT 
     COUNT(DISTINCT d2.orderNo) as orderNoCt 
    FROM mck_hvs.oldorderdetails d2 with (nolock) 
    WHERE 
     d2.packingoperator = d.packingoperator 
    AND d2.packingunit = d.packingunit 
    and DATEPART(hh, d2.datetimepacked) = DATEPART(hh, d.datetimepacked) 
) ct 
CROSS APPLY 
    (
    SELECT 
     DISTINCT d3.orderNo as orderNoDt 
    FROM mck_hvs.oldorderdetails d3 with (nolock) 
    WHERE 
     d3.packingoperator = d.packingoperator 
    AND d3.packingunit = d.packingunit 
    and DATEPART(hh, d3.datetimepacked) = DATEPART(hh, d.datetimepacked) 
) dt 

WHERE 
    CONVERT(DATE, d.datetimepacked) = CONVERT(DATE, @date) 
GROUP BY 
    d.packingoperator 
    ,DATEPART(hh, d.datetimepacked) 
    ,d.packingunit 
ORDER BY 
    d.packingoperator 
    ,DATEPART(hh, d.datetimepacked)