我需要一些帮助来查询SUM不同的材料。
材料名称数量日期
A ...................... 20 .... 1/1/2010
A ...................... 10 .... 1/2/2010
B ...................... 30 .... 1/2/2010
C ...................... 20 .... 1/3/2010
例如我只想总结材料A和B.
答案 0 :(得分:0)
SELECT SUM(Quantity) FROM TABLE WHERE MaterialName in ('A', 'B')
OR
SELECT MaterialName , SUM(Quantity) FROM TABLE
WHERE MaterialName in ('A', 'B') GROUP BY MaterialName
答案 1 :(得分:0)
A,B的计数
select sum(case when MaterialName='A' then 1 else 0 end) as CountA,
sum(case when MaterialName='B' then 1 else 0 end) as CountB,
GROUP BY Date
如果您的意思是数量总和
SELECT SUM(Quantity),MaterialName FROM TABLE WHERE MaterialName in ('A', 'B')
GROUP BY MaterialName
答案 2 :(得分:0)
试试这个。添加where clause
以过滤A
& B
然后SUM
每MaterialName
SELECT MaterialName,
Sum(Quantity) Sum_Quantity
FROM TableName
WHERE MaterialName IN ( 'A', 'B' )
GROUP BY MaterialName