2012-11-16 44 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蘇姆小計。再次感謝您的幫助