简体   繁体   中英

PIVOT TABLE - That gets the grand total for the month

I have the code written by TomT and here is the SQLFiddle: - Many Thanks to TomT.

SQL Fiddle

How do i add a new column that gets the Grand TotalLoad for the month.

Here is what i am Looking from the above fiddle:

Column Headers:

Month | GrandTotalforthe month | On Peak TotalLoad | Off Peak TotalLoad | onPeak MaxLoad | OffPeak MaxLoad|

Row Header is based on the Month: Jan : 202.0869+166.6052= 368.6921 (GrandTotalforthemonth) | Onpeak TotalLoad for the Month= 202.0869 | OffPeak TotalLoad for the month = 166.6052 | OnPeak Maxload for the month = 0.9987 and OffPeakLoad for the Month= 0.9956

your last select needs to use 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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM