2017-08-28 127 views
0

我寫了一個查詢來獲得三個字段的總和,除了大多數總和爲零。我只需要返回大於零的總和。下面是查詢SQL總和,返回字段不是零

SELECT [Product], 
SUM([Pounds]) as SumPounds, 
SUM([Dollars]) as SumDollars, 
SUM([EBITDA]) as SumEBITDA 
FROM MyTable 
WHERE ([Year] = '2017' AND [Date] <= #8/20/2017#) 
SORT BY [Product]; 

有沒有辦法只返回大於零的總和在Access?

回答

2
SELECT [Product], 
     SUM([Pounds]) as SumPounds, 
     SUM([Dollars]) as SumDollars, 
     SUM([EBITDA]) as SumEBITDA 
FROM MyTable 
WHERE ([Year] = '2017' AND [Date] <= #8/20/2017#) 
GROUP BY [Product] 
HAVING SUM([Pounds]) + SUM([Dollars]) + SUM([EBITDA]) > 0 
+2

或者'Sum([Pounds] + [Dollars] + [EBITDA])> 0'稍微更緊湊(假設沒有NULL) – ErikE

+1

Way better .... –

+0

你是上帝,我不是。多謝! – jDave1984

1

可以使用具有過濾器的聚合函數的結果

SELECT [Product], 
SUM([Pounds]) as SumPounds, 
SUM([Dollars]) as SumDollars, 
SUM([EBITDA]) as SumEBITDA 
FROM MyTable 
WHERE ([Year] = '2017' AND [Date] <= #8/20/2017#) 
HAVING UM([Pounds]) >0 AND SUM([Dollars]) > 0 AND SUM([EBITDA]) >0 
SORT BY [Product]; 

,如果你需要的所有3值以上,如果你只需要一個,你可以使用或

可以使用並作爲答案