简体   繁体   English

PIVOT TABLE-得出该月的总计

[英]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. 我有TomT编写的代码,这里是SQLFiddle:-非常感谢TomT。

SQL Fiddle SQL小提琴

How do i add a new column that gets the Grand TotalLoad for the month. 如何添加新列以获取当月的Grand TotalLoad。

Here is what i am Looking from the above fiddle: 这是我从上面的小提琴看的内容:

Column Headers: 列标题:

Month | 月| GrandTotalforthe month | 月份总计 On Peak TotalLoad | 高峰时TotalLoad | Off Peak TotalLoad | 非高峰期总负荷| onPeak MaxLoad | onPeak MaxLoad | OffPeak MaxLoad| OffPeak MaxLoad |

Row Header is based on the Month: Jan : 202.0869+166.6052= 368.6921 (GrandTotalforthemonth) | 行标题基于月份:1月:202.0869 + 166.6052 = 368.6921(月总计)| Onpeak TotalLoad for the Month= 202.0869 | 本月的Onpeak总负载= 202.0869 | OffPeak TotalLoad for the month = 166.6052 | 该月的OffPeak总负载= 166.6052 | OnPeak Maxload for the month = 0.9987 and OffPeakLoad for the Month= 0.9956 该月的OnPeak最大负载= 0.9987,该月的OffPeakLoad = 0.9956

your last select needs to use conditional aggregation 您的最后选择需要使用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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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