久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

數據透視表 SQL 中的水平總計

Horizontal Grand Total in Pivot Table SQL(數據透視表 SQL 中的水平總計)
本文介紹了數據透視表 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

我想在同一個查詢中放入 Horizo??ntal Total示例:

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

這個怎么做?我用 UNION 讀過一些東西.

How to make this? I have read something with UNION.

推薦答案

首先,您不需要事先對數據進行分組:PIVOT 子句會為您做到這一點.因此,您可以刪除 GROUP BY 子句并相應地更改 PIVOT 中 SUM() 的參數:

First of all, you don't need to group your data beforehand: the PIVOT clause will do that for you. So you can remove the GROUP BY clause and change the SUM()'s argument in PIVOT accordingly:

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;

要添加總計列,您可以使用 window SUM() 像這樣:

To add a total column, you could use a window SUM() like this:

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 行集,如下所示:

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. So you might want to filter the sourcetable row set before pivoting, like this:

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;

這篇關于數據透視表 SQL 中的水平總計的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 91大神在线看| 日本电影一区二区 | 一级片在线观看 | 午夜精品一区 | 天天干b | 久久久久久久一区二区三区 | 亚洲精品一区二三区不卡 | 男女羞羞在线观看 | 一级黄色毛片 | 亚洲成人一区二区三区 | 国产精品一二三区 | 国产福利91精品 | 一级片av| 永久精品| 午夜视频在线观看一区二区 | 日韩欧美在线一区二区 | 天天爽天天操 | 精品国产一区二区三区久久影院 | 日本精品视频 | 日本三级日产三级国产三级 | 国产亚洲精品美女久久久久久久久久 | 欧美国产视频 | 日本精品网站 | 国产91av视频在线观看 | 中文字幕在线中文 | 国产高清视频在线观看 | 欧美日韩电影一区二区 | 成人在线视频免费观看 | 午夜视频在线观看视频 | 请别相信他免费喜剧电影在线观看 | 99精品国产在热久久 | 国产伦精品一区二区三区高清 | 国产精品久久久久久久一区探花 | 国产精品视频在线播放 | 91网站在线看 | 日韩图区| 欧美性网| 精品视频一区二区三区 | 日本在线播放一区二区 | 亚洲精品视频免费看 | 久久久久国色av免费观看性色 |