开发者

SQL: How to return any Part that occurs more than Once

I have the following query which returns the following error: An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.

SELECT Pa开发者_如何学Gort from Parts Where count(Part) > 1

How could i rewrite it to return the part that appears more than once.


You need to use a GROUP BY and HAVING clause like this:

SELECT part
FROM Parts
GROUP BY part
HAVING COUNT(*) > 1


A perfect opportunity for the rarely used HAVING clause:

SELECT Part, Count(Part) as PartCount
FROM Parts
GROUP BY Part
HAVING Count(Parts) > 1


try this:

select part from parts group by part having count(part) > 1

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜