3

我有这个查询工作:

      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;

此查询返回:

      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

我想在同一个查询中输入水平总计示例:

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

这个怎么做?我读过一些东西UNION

4

2 回答 2

6

首先,您不需要事先对数据进行分组:PIVOT 子句将为您完成。因此,您可以删除 GROUP BY 子句并相应地更改SUM()PIVOT 中的 ' 参数:

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;

要添加总计列,您可以使用如下窗口 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;

但是请注意,如果您sourcetable包含的行的cap_idEmpresa值不是 PIVOT 子句中列出的值,则相应的cap_valorfatura值也将相加。因此,您可能希望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;
于 2012-11-16T15:54:05.240 回答
1

感谢大家,这是最后的查询:

 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;

我需要使用 isnull 将 NULL 更改为 o 以 sume subtotal 。再次感谢帮助

于 2012-11-19T13:16:00.503 回答