2016-01-07 47 views
1

我想將值分組並將它們按類別值進行彙總 - 或者不加上。 例如,我有如下表:GROUP BY,將每個組的空值求和

+-------+----------+ 
| value | category | 
+-------+----------+ 
| 99 |  A | 
| 42 |  A | 
| 76 | [NULL] | 
| 66 |  B | 
| 10 |  C | 
| 13 | [NULL] | 
| 27 |  C | 
+-------+----------+ 

我想要的結果應該是這樣的:

+-------+----------+ 
| sum | category | 
+-------+----------+ 
| 230 |  A | 
| 155 |  B | 
| 126 |  C | 
| 89 | [NULL] | 
+-------+----------+ 

我嘗試了group by category但顯然這並不帶來了正確的數字。

任何想法?

我使用SQL Server 2012的

編輯: 好了,按照要求,我可以解釋我的意圖,並給我查詢,到目前爲止,雖然不是非常有幫助,我認爲。

我需要總結的所有值對於給定的類別和所有值相加的總和沒有分類的[=> NULL] 所以在我的例子,我會總結

99 + 42 + 76 + 13 = 230 for category A 
66 + 76 + 13 = 155  for category B 
10 + 27 + 76 + 13 = 126 for category C 
76 + 13 = 89   for no category 

我希望給你我的目標的想法。

查詢至今:

SELECT SUM([value]), [category] 
FROM [mytable] 
GROUP BY [category] 
+0

能否請您顯示當前查詢? – levelonehuman

+0

爲什麼應該將'sum'對應'null'類別添加到所有組合數中? –

回答

2

首先計算出空位的總和,然後將其加到每組中:

DECLARE @t TABLE 
    (
     value INT , 
     category CHAR(1) 
    ) 

INSERT INTO @t 
VALUES (99, 'A'), 
     (42, 'A'), 
     (76, NULL), 
     (66, 'B'), 
     (10, 'C'), 
     (13, NULL), 
     (27, 'C') 

;with cte as(select sum(value) as s from @t where category is null) 
select category, sum(value) + s 
from @t 
cross join cte 
where category is not null 
group by category, s 

另一個版本:

;WITH cte AS(SELECT category, SUM(value) OVER(PARTITION BY category) + 
       SUM(CASE WHEN category IS NULL THEN value ELSE 0 END) OVER() AS value 
      FROM @t) 
SELECT DISTINCT * FROM cte WHERE category IS NOT NULL 
+0

這看起來很有前途。我會在我的(現實中更復雜的)查詢中嘗試它,並將其標記爲解決方案。無論如何,複製/粘貼工作示例爲+1。 – z00l

0

如果你想在NULL值添加到所有的組,然後像做:

with cte as (
     select category, sum(value) as sumv 
     from t 
     group by category 
    ) 
select cte.category, 
     (cte.sumv + 
     (case when category is not null then coalesce(ctenull.sumv) else 0 end) 
     ) as sumWithNulls 
from cte left join 
    cte ctenull 
    on ctenull.category is null  -- or should that be `= '[NULL]'`? 

這似乎是一個奇怪的操作。

編輯:

你幾乎可以做到這一點與窗口函數:

select category, 
     (sum(value) + 
     sum(case when category is null then sum(value) else 0 end) over() 
     ) as sumWithNulls 
from t 
group by category; 

的問題是,NULL小號克服計入該類別。所以:

select category, 
     (sum(value) + 
     (case when category is not null 
       then sum(case when category is null then sum(value) else 0 end) over() 
       else 0 
     end 
     ) as sumWithNulls 
from t 
group by category; 
0

你想要得到的NULL類別的總和,並把它添加到其他(非空)類別的值:

DECLARE @Table1 TABLE (Value int, Category varchar(1)) 

DECLARE @NullCategorySum int 

INSERT INTO @Table1 
    (Value, Category) 
VALUES 
    (99, 'A'), 
    (42, 'A'), 
    (76, NULL), 
    (66, 'B'), 
    (10, 'C'), 
    (13, NULL), 
    (27, 'C') 

SELECT @NullCategorySum = SUM(Value) 
FROM @Table1 
WHERE Category IS NULL 

SELECT SUM(t1.Value) 
      + CASE 
       WHEN Category IS NOT NULL THEN @NullCategorySum 
       END 
     AS SumValue, Category 
FROM @Table1 t1 
GROUP BY Category 

此輸出

SumValue Category 
89   NULL 
230   A 
155   B 
126   C 
0

也許你使用SUM內置函數只是錯過了什麼?這應該工作:

SELECT 
SUM(value) AS [sum], category 
FROM 
[YourTableHere] 
GROUP BY category 

編輯:啊,我明白你現在正在做什麼。我能夠通過僅使用NULL總和來加入第二個查詢來完成它,所以NULL和會隨着每一行返回。然後你可以在最後一步添加它。

SELECT 
MainSet.sum + JustNulls.sum AS [sum], MainSet.category 
FROM 

(SELECT SUM(X.value) AS [sum], X.category FROM [YourTableHere] X 
WHERE X.category IS NOT NULL GROUP BY category 
UNION SELECT 0, NULL) MainSet 

FULL JOIN 

(SELECT SUM(Y.value) AS [sum], Y.category FROM [YourTableHere] Y 
WHERE Y.category IS NULL GROUP BY category) JustNulls ON 1=1 
0

類似levelonehuman把可能快一點

declare @countNull int = (select sum(textUniqueWordCount) from docSVsys where mimeType is null); 
select mimeType, sum(isnull(textUniqueWordCount, 0)) + @countNull as [sum] 
from docSVsys 
where mimeType is not null 
group by mimeType 
union 
select null, @countNull;