我試圖在數據透視的幫助下找到平均值,但無法找到正確的解決方案。以數據爲中心計算平均值
下面是我的查詢:
select branch, ISNULL([11:00], 0) as [11:00],ISNULL([11:15], 0) as
[11:15],ISNULL([11:30], 0) as [11:30], ISNULL([11:45], 0) as [11:45],
ISNULL([12:00], 0) as [12:00]
from
(
select b.branchname
,convert(varchar(5), intervals.interval_start_time, 108)
,sum(b.ordercount) ordercounts
from branch b cross apply dbo.getDate15MinInterval(CAST(b.TransactionDate
as date)) as intervals
where b.TransactionDate >= interval_start_time and b.TransactionDate <=
interval_end_time
and CAST(TransactionDate AS date) IN ('2017-07-01','2017-07-08')
group by DATEPART(WEEKDAY,TransactionDate),b.branchname,intervals.interval_start_time,intervals.interval_end_time
) t
pivot (avg(ordercounts) for interval_start_time in ([11:00], [11:15] ,
[11:30], [11:45], [12:00])) as p
我原來的表是:從上面的查詢
結果是:
預期的結果:
對於15minuteinterval查詢,請參考我原來的職位:
Group data by interval of 15 minutes and use cross tab
嗨戈登,那只是漂浮的作品。但是我仍然在輸出中得到1和8。它應該是0.5和4。 – Shivang