我如何总结一组总和? SQL Server 2008

时间:2011-03-08 15:39:35

标签: sql-server-2008 group-by sum inner-join

我的查询中包含sum,如下所示:

SELECT
    Table1.ID, SUM(Table2.[Number1] + Table2.[Number2]) AS SumColumn
FROM         Table1 INNER JOIN
                      Table3 ON Table1.ID = Table3.ID 
                    INNER JOIN
                      Table2 ON Table3.ID = Table2.ID
WHERE     (Table2.[Something] = 'Whatever')

GROUP BY Table1.ID,  Table2.[Number1] , Table2.[Number2]

它给了我一张这样的表格:

ID  SumColumn
67  1
67  4
70  2
70  6
70  3
70  6
80  5
97  1
97  3

如何让它给我一个这样的表,SumColumn总和,按ID列分组?

ID   SumColumn
67   5
70   17
80   5
97   4

我不能GROUP BY SumColumn因为我收到错误(无效的列名'SumColumn'。)COALESCE也不起作用。提前谢谢。

修改

只是按ID分组会给我一个错误:

[Number1,Number2和我选择的其他列名]在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。

编辑2

不知道为什么只是按Table.ID进行分组现在似乎有效。感谢所有发布正确答案的人,我希望我能把它们全部打勾!

4 个答案:

答案 0 :(得分:4)

你试过了吗?

SELECT
    Table1.ID, SUM(Table2.[Number1] + Table2.[Number2]) AS SumColumn
FROM         Table1 INNER JOIN
                      Table3 ON Table1.ID = Table3.ID 
                    INNER JOIN
                      Table2 ON Table3.ID = Table2.ID
WHERE     (Table2.[Something] = 'Whatever')
GROUP BY Table1.ID

我不明白为什么上面的内容不起作用,除非你没有在查询中适当地对表进行别名,这更多的是语法而不是逻辑错误,但是从SQL引擎的角度来看是“错误的”。每当SQL代码对我不起作用时,我会简化 ad nauseam 我的查询,直到它失败的原因变得明显。在那种情况下,我会尝试:

SELECT ID, SUM(sumCol) as SumColumn
FROM (
    SELECT
        Table1.ID, (Table2.[Number1] + Table2.[Number2]) AS sumCol
    FROM         Table1 INNER JOIN
                          Table3 ON Table1.ID = Table3.ID 
                        INNER JOIN
                          Table2 ON Table3.ID = Table2.ID
    WHERE     (Table2.[Something] = 'Whatever')
)
GROUP BY Table1.ID

...我允许从以下(和嵌套查询!)中显示的任何错误通知我的进一步调查。

答案 1 :(得分:4)

听起来你只需要通过Table1.ID进行分组。

SELECT
    Table1.ID, SUM(Table2.[Number1] + Table2.[Number2]) AS SumColumn
FROM         Table1 INNER JOIN
                      Table3 ON Table1.ID = Table3.ID 
                    INNER JOIN
                      Table2 ON Table3.ID = Table2.ID
WHERE     (Table2.[Something] = 'Whatever')

GROUP BY Table1.ID

由于SUM()是一个聚合函数,它会处理将数字组合在一起,你只需要指定如何对你想要的其他列进行分组。

[UPDATE]

演示代码:

CREATE TABLE #T1(
    ID      INT
)
CREATE TABLE #T2 (
    ID              INT,
    Something       VARCHAR(32),
    Number1         INT,
    Number2         INT
)
CREATE TABLE #T3 (
    ID      INT
)
DECLARE @Index INT = 0

WHILE @Index < 50
BEGIN
    DECLARE @Something VARCHAR(32) = ''

    SET @Index = @Index + 1
    IF @Index BETWEEN 0 AND 15
        SET @Something = 'Blah'
    ELSE IF @Index BETWEEN 15 AND 40
        SET @Something = 'Whatever'
    ELSE IF @Index BETWEEN 40 AND 50
        SET @Something = 'Bleh'
    INSERT INTO #T1 VALUES(@Index)
    INSERT INTO #T3 VALUES(@Index)
    INSERT INTO #T2 VALUES(@Index, @Something, RAND()*100, RAND() * 100)
    INSERT INTO #T2 VALUES(@Index, @Something, RAND()*100, RAND() * 100)
    INSERT INTO #T2 VALUES(@Index, @Something, RAND()*100, RAND() * 100)
END

SELECT
    #T1.ID, SUM(#T2.Number1 + #T2.Number2) AS SumColumn
FROM         #T1 INNER JOIN
                      #T3 ON #T1.ID = #T3.ID 
                    INNER JOIN
                      #T2 ON #T3.ID = #T2.ID
WHERE     (#T2.Something = 'Whatever')
GROUP BY #T1.ID

答案 2 :(得分:2)

试试这个?

SELECT
    Table1.ID, SUM(Table2.[Number1] + Table2.[Number2]) AS SumColumn
FROM         Table1 INNER JOIN
                      Table3 ON Table1.ID = Table3.ID 
                    INNER JOIN
                      Table2 ON Table3.ID = Table2.ID
WHERE     (Table2.[Something] = 'Whatever')

GROUP BY Table1.ID

答案 3 :(得分:1)

您是否尝试过嵌套选择?

SELECT ID, SUM(SumColumn) AS SumColumn
FROM (SELECT Table1.ID AS ID, SUM(Table2.[Number1] + Table2.[Number2]) AS SumColumn
      FROM Table1 
           INNER JOIN Table3 ON Table1.ID = Table3.ID 
           INNER JOIN Table2 ON Table3.ID = Table2.ID
      WHERE (Table2.[Something] = 'Whatever')
      GROUP BY Table1.ID,  Table2.[Number1] , Table2.[Number2])
GROUP BY ID