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)
你剛剛標記了這個每個包含「sql」的標籤嗎? – Uueerdo
看到如何只有四個標籤,我不明白你的關注。這個問題屬於他們的範疇,那麼我爲什麼不呢? @Uueerdo –
MySQL和SQL Server通常可以有非常不同的答案。沒有內置GROUP_CONCAT的MySQL沒有CTE和MSSQL是立即想到的兩件事情。出於某種原因,示例中的樣式讓我想起了MySql,但'nolock'的存在暗示了MSSQL。 – Uueerdo