2012-11-16 43 views
3

我有这样的查询工作:水平总计透视表SQL

 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

+0

它不清楚你如何获得总计行?你能用一个工作数据模型创建一个[sql小提琴](http://sqlfiddle.com/),然后澄清你的问题吗? – Taryn

回答

3

首先,您不需要事先对您的数据进行分组: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; 

要添加一个总列,你可以使用一个windowSUM()这样的:

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; 
+0

这工作完美Andriy男,很多谢谢,我会上传架构在sqlFiddle后,其他问题,我写的订单?我想按顺序排列输出cap_idPlanoContasFin –

+0

只需将它放在查询的末尾,在'as pivottable'之后,即像这样:'... as pivottable ORDER BY cap_idPlanoContasFin;'。 –

+0

它的posible仅使列22345和2的小计结果? –

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苏姆小计。再次感谢您的帮助