简体   繁体   English

数据透视表SQL中的水平总计

[英]Horizontal Grand Total in Pivot Table SQL

I have this query working: 我有这个查询工作:

      select cap_idPlanoContasFin  , [3684],[2234],[2] ,  
      from 
      (
      select cap_idPlanoContasFin,cap_idempresa,sum(cap_valorfatura) 
          as Stotal    
          from erp_ContasPagar 
       group by cap_idPlanoContasFin , cap_idEmpresa 

       ) as sourcetable
       pivot 
       (sum(Stotal)for cap_idEmpresa in ([3684],[2234],[2])
       )as pivottable;

This query returns: 此查询返回:

      cap_idPlanoContasFin  3684          2234       2
                      3 9000          NULL      NULL
                     10 1057840,68    NULL  1865081,35
                     11 NULL          7283,1    591,9
                     12 NULL          NULL  178914,45
                     13 9305,07       1117,6    500
                     14 NULL          59333,5   34611,74

I want to put in the same query the Horizontal Total Example: 我想在水平总示例中输入相同的查询:

      cap_idPlanoContasFin  3684      2234            2           Total
      ---------------------------------------------------------------------      
                       13   9305,07    1117,6   500          10922,67

How to make this? 怎么做到这个? I have read something with UNION . 我用UNION读过一些东西。

First of all, you don't need to group your data beforehand: the PIVOT clause will do that for you. 首先,您不需要事先对数据进行分组:PIVOT子句将为您执行此操作。 So you can remove the GROUP BY clause and change the SUM() 's argument in PIVOT accordingly: 因此,您可以删除GROUP BY子句并相应地更改PIVOT中的SUM()参数:

select cap_idPlanoContasFin, [3684], [2234], [2]  
from 
(
  select cap_idPlanoContasFin, cap_idempresa, cap_valorfatura
    from erp_ContasPagar 
  group by cap_idPlanoContasFin , cap_idEmpresa
) as sourcetable
pivot 
(
  sum(cap_valorfatura) for cap_idEmpresa in ([3684], [2234], [2])
) as pivottable;

To add a total column, you could use a window SUM() like this: 要添加总列,可以使用如下窗口 SUM()

select cap_idPlanoContasFin, [3684], [2234], [2], Total
from 
(
  select cap_idPlanoContasFin, cap_idempresa, cap_valorfatura, sum(cap_valorfatura) over (partition by cap_idPlanoContasFin) as Total
    from erp_ContasPagar 
) as sourcetable
pivot 
(
  sum(cap_valorfatura) for cap_idEmpresa in ([3684], [2234], [2])
) as pivottable;

Note, however, that if your sourcetable includes rows with cap_idEmpresa values other than those listed in the PIVOT clause, the corresponding cap_valorfatura values will be added up too. 但请注意,如果您的sourcetable包含的cap_idEmpresa值不是PIVOT子句中列出的值,则相应的cap_valorfatura值也会相加。 So you might want to filter the sourcetable row set before pivoting, like this: 因此,您可能希望在sourcetable之前过滤源表行集,如下所示:

select cap_idPlanoContasFin, [3684], [2234], [2], Total
from 
(
  select cap_idPlanoContasFin, cap_idempresa, cap_valorfatura,
         sum(cap_valorfatura) over (partition by cap_idPlanoContasFin) as Total
    from erp_ContasPagar 
   where cap_idempresa in (3684, 2234, 2)
) as sourcetable
pivot 
(
  sum(cap_valorfatura) for cap_idEmpresa in ([3684], [2234], [2])
) as pivottable;

Thanks to all , this is the final query : 谢谢大家,这是最后的查询:

 select cap_idPlanoContasFin, plc_classificador, plc_nomeConta,[3684], [2234], [2], 
 isnull ([2234],0) + isnull ([2],0) AS Subtotal ,Total    
 from 
 (
 select A.cap_idempresa, A.cap_idPlanoContasFin,  A.cap_valorfatura, 
 B.plc_classificador , B.plc_nomeConta,
 sum(A.cap_valorfatura) over (partition by A.cap_idPlanoContasFin) as Total
 from erp_ContasPagar A /*where cap_idempresa in (3684, 2234, 2)*/ 
 inner  join  tbl_PlanoFinanceiro B on A.cap_idPlanoContasFin = B.plc_id
 )  as sourcetable
 pivot 
 (
 sum(cap_valorfatura) for cap_idEmpresa in ([3684], [2234], [2])
 ) as pivottable;

I need use isnull to change NULL by o to sume subtotal . 我需要使用isnull来将NULL更改为su到sume小计。 Thanks again by the help 再次感谢您的帮助

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

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