sum()返回多行

时间:2018-10-05 13:17:33

标签: sql sql-server-2012

我正在尝试获取所有负值的总和,所有正值的总和。这是我的查询:

SELECT
        gl.rowno,
        gl.br
        gl.fs
        gl.cudic
        gl.name
        gl.no_
        gl.balance
        SUM(glhi.amount) AS Total,
        CASE WHEN glhi.amount >0 THEN ISNULL(SUM(glhi.amount),0) END AS TotalPositive,
        CASE WHEN glhi.amount <0 THEN ISNULL(SUM(glhi.amount),0) END AS TotalNegative
FROM gl
        INNER JOIN glhi ON gl.rowno = glhi.rowno
WHERE 
        status = 'active'
        AND glhi.amount != 0.00
        AND glhi.effective BETWEEN '09-01-2017' AND '09-30-2017'
GROUP BY gl.rowno, gl.name, gl.no_, gl.branch, gl.fs, gl.cudic, gl.balance, glhi.effective, glhi.amount
ORDER BY gl.br, gl.name

这是我得到的当前输出的示例:

enter image description here

编辑:显而易见的答案是从我的GROUP BY子句中删除glhi.amount,但是当我这样做时,我得到了这个错误:

Column 'glhi.amount' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

5 个答案:

答案 0 :(得分:2)

当您使用sumavgcount等聚合函数时,似乎是正确的结果。必须对值进行分组才能进行正确的聚合

在这种情况下,您将根据此条件对结果进行分组 GROUP BY gl.rowno, gl.name, gl.no_, gl.branch, gl.fs, gl.cudic, gl.balance, glhi.effective, glhi.amount

您的字段glhi.amount在表中具有不同的值,因此此查询将为您提供多个记录

此外,请注意,在这种情况下,您应该将和放在sum函数中

CASE WHEN glhi.amount >0 THEN ISNULL(SUM(glhi.amount),0) END AS TotalPositive,
CASE WHEN glhi.amount <0 THEN ISNULL(SUM(glhi.amount),0) END AS TotalNegative

更改为

sum(case when glhi.amount > 0 then coalesce(glhi.amount, 0)) end as TotalPositive
sum(case when glhi.amount < 0 then coalesce(glhi.amount, 0)) end as TotalNegative

答案 1 :(得分:1)

根据输出数据,您需要删除glhi.amount中的GROUP BY

SELECT
        gl.rowno,
        gl.br
        gl.fs
        gl.cudic
        gl.name
        gl.no_
        gl.balance
        SUM(glhi.amount) AS Total,
        CASE WHEN glhi.amount >0 THEN ISNULL(SUM(glhi.amount),0) END AS TotalPositive,
        CASE WHEN glhi.amount <0 THEN ISNULL(SUM(glhi.amount),0) END AS TotalNegative
    FROM gl
        INNER JOIN glhi ON gl.rowno = glhi.rowno
    WHERE 
        status = 'active'
        AND glhi.amount != 0.00
        AND glhi.effective BETWEEN '09-01-2017' AND '09-30-2017'
        GROUP BY gl.rowno, gl.name, gl.no_, gl.branch, gl.fs, gl.cudic, gl.balance, glhi.effective
        ORDER BY gl.br, gl.name

答案 2 :(得分:1)

我认为最好的逻辑是:

sum(case when glhi.amount > 0 then glhi.amount else 0 end) as TotalPositive
sum(case when glhi.amount < 0 then glhi.amount else 0 end) as TotalNegative

这称为“条件聚集”。 NULL比较不是必需的; else 0可以解决这个问题。

答案 3 :(得分:0)

如果每个其他项目有多个glhi.amount,它将返回多行。尝试像这样删除glhi.amount

SELECT
        gl.rowno,
        gl.br
        gl.fs
        gl.cudic
        gl.name
        gl.no_
        gl.balance
        SUM(glhi.amount) AS Total,
        CASE WHEN glhi.amount >0 THEN ISNULL(SUM(glhi.amount),0) END AS TotalPositive,
        CASE WHEN glhi.amount <0 THEN ISNULL(SUM(glhi.amount),0) END AS TotalNegative
    FROM gl
        INNER JOIN glhi ON gl.rowno = glhi.rowno
    WHERE 
        status = 'active'
        AND glhi.amount != 0.00
        AND glhi.effective BETWEEN '09-01-2017' AND '09-30-2017'
        GROUP BY gl.rowno, gl.name, gl.no_, gl.branch, gl.fs, gl.cudic, gl.balance, glhi.effective
        ORDER BY gl.br, gl.name

答案 4 :(得分:0)

处理2个案例并将其放在SUM()子句中,然后可以删除GROUP BY金额并生效。谢谢大家的帮助

select DISTINCT
    gl.rowno,
    gl.br
    gl.fs 
    gl.cudic,
    gl.name
    gl.no_
    gl.balance 
    SUM(glhi.amount) AS Total,
    SUM(CASE WHEN glhi.amount >0 THEN glhi.amount END)AS TotalPositive,
    SUM(CASE WHEN glhi.amount <0 THEN glhi.amount END)AS TotalNegative
FROM gl
    INNER JOIN glhi ON gl.rowno = glhi.rowno
WHERE 
    status = 'active'
    AND glhi.amount != 0.00
    AND glhi.effective BETWEEN '09-01-2017' AND '09-30-2017'
    GROUP BY gl.rowno, gl.name, gl.no_, gl.br, gl.fs, gl.cudic, gl.balance
ORDER BY branch, name