如何在一列有多个条目的列上使用group by?

时间:2013-04-17 09:33:28

标签: sql sql-server tsql group-by

我想在一个包含许多条目的列上使用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因为0price

4 个答案:

答案 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 BYHAVING子句来实现此目的:

SQLFiddle Demo

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