如果我有桌子,比如10列。 8列数据和2列(1和2)作为标识符。
我想为col1和col2的每种可能情况计算8列的“结果”。
假设distinct(value)col1给出{A,B,C}和distinct(value)col2给出{D,E,F}我想总结数据(8列中的任何一列)GROUP'd BY :
答案 0 :(得分:3)
SELECT
SUM(colx) OVER (PARTITION BY Col1) AS [All of col1],
SUM(colx) OVER (PARTITION BY Col2) AS [All of col2],
SUM(colx) OVER (PARTITION BY Col1, Col2) AS [PerCol1Col2Pair],
SUM(colx) OVER () AS [All of col1 and All of col2]
FROM
MyTable
答案 1 :(得分:0)
在我看来,你想要GROUP BY CUBE()
(见GROUP BY (Transact-SQL)):
SELECT
col1,
col2,
COUNT(*) AS cnt
FROM atable
GROUP BY CUBE(col1, col2)
这将产生如下结果:
col1 col2 cnt ---- ---- -------- A D xAD A E xAE A … … B D xBD B E xBE B … … A NULL xAD + xAE + … B NULL xBD + xBE + … NULL D xAD + xBD + … NULL E xAE + xBE + … … … … NULL NULL xAD + xAE + … + xBD + xBE + …