我有TomT编写的代码,这里是SQLFiddle: - 非常感谢TomT。
如何添加一个获得本月Grand TotalLoad的新列。
以下是我从上面的小提琴中看到的内容:
列标题:
月| GrandTotalforthe月|在峰值TotalLoad | Off Peak TotalLoad | onPeak MaxLoad | OffPeak MaxLoad |
行标题基于月份: Jan:202.0869 + 166.6052 = 368.6921(GrandTotalforthemonth)| Onpeak TotalLoad for the Month = 202.0869 |本月OffPeak TotalLoad = 166.6052 |月份的OnPeak Maxload = 0.9987,月份的OffPeakLoad = 0.9956
答案 0 :(得分:0)
您最后的选择需要使用conditional aggregation
select [month], sum([load]) as GrandTotal,
sum( case when [onpeak] =0 then [load] end) as OnPeakTotalLoad,
sum( case when [onpeak] =1 then [load] end) as OffPeakTotalLoad,
max( case when [onpeak] =0 then [load] end) as OnPeakMaxLoad,
max( case when [onpeak] =1 then [load] end) as OffPeakMaxLoad
from ReadingMonthPeak
group by [month]
order by [month]