4

我想在有很多条目的一列上使用 group by

表_a

Name price
AAA  12
BBB  13
AAA  0
CCC  24
AAA  0
DDD  0

现在我想知道Name哪个Price0

但由于我有AAA3 次条目,我不能直接用条件编写简单的 sql NOT Equal to 0

请帮助我我想打印上面 table_a 的结果应该是

只是D因为它具有0as price

4

4 回答 4

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
于 2013-04-17T09:35:11.560 回答
2

您可以通过使用聚合函数GROUP BYHAVING子句来实现这一点:

SQLFiddle 演示

SELECT name,SUM(price) as price
FROM table 
GROUP BY name
HAVING SUM(price) = 0
于 2013-04-17T09:36:16.843 回答
1
select name,max(price) as price from table group by name having max(price) =0
于 2013-04-17T09:36:11.123 回答
1
select name,max(price)
group by name
having max(price)=0
于 2013-04-17T09:37:07.120 回答