我有一个SQL视图名称dbo.financialactuals
ACCTNAME Month Balance Acctctgry Year Description
sales 1 344.78 income 2018 revenueAX
sales 2 2744.78 income 2018 ProduceAX
sales 3 8745.78 income 2018 annualAx
INTEREST INC 1 7866 Interest 2018 ProduceAX
INTEREST INC 2 766 Interest 2018 CTGAX3
sales 5 744.78 other 2018 AX
我试图通过AcctName对余额进行求和,但是仍然显示相同的结果。我认为是因为描述列。因此,我从select语句中删除了描述,但它的作用相同。
SELECT AcctName,
SUM(Balance) AS Balance,
AcctCtrgry,
Year,
FROM dbo.financialactuals
GROUP BY AcctName, Acctctrgy, Year,
我需要这样的输出。
AcctNAme Balance AcctCtrgy Year
sales -3089.56 income 2018
INTEREST INC 9632 InterestINC 2018
答案 0 :(得分:0)
正如评论中已经提到的那样,这应该给出所请求的结果。
SELECT AcctName,
SUM(Balance) AS Balance,
AcctCtrgry,
Year,
FROM dbo.financialactuals
GROUP BY AcctName, Acctctrgy, Year
但是销售总额为-11835.34,而不是-3089.56。