我想在一个包含许多条目的列上使用group by
表-A
Name price
AAA 12
BBB 13
AAA 0
CCC 24
AAA 0
DDD 0
现在我想找出Name
Price
作为0
但是由于我有条目AAA
3次,我无法直接用条件编写简单的sql
NOT Equal to 0
请帮帮我,我想打印上面的表格_a应该是
仅D
因为0
为price
。
答案 0 :(得分:3)
我假设价格不能低于零:
SELECT Name
FROM TableName
GROUP BY Name
HAVING SUM(price) = 0
或者只有一个价格的附加条件,即零
SELECT Name
FROM TableName
GROUP BY Name
HAVING COUNT(*) = 1 AND SUM(price) = 0
答案 1 :(得分:2)
您可以使用聚合函数GROUP BY
和HAVING
子句来实现此目的:
SELECT name,SUM(price) as price
FROM table
GROUP BY name
HAVING SUM(price) = 0
答案 2 :(得分:1)
select name,max(price) as price from table group by name having max(price) =0
答案 3 :(得分:1)
select name,max(price)
group by name
having max(price)=0