开发者

Cannot produce query to find multiple under same member record

I have the following sample table:

 ID    |     Code
=================
1      |     123
2      |     123
2      |     456
2      |     456
2      |     789
3      |     123
3      |     789

I want to return the ID and code in which the same code appears for a single ID.

The results for the desired query based on the table above would be:

ID   |    Code
==============
2    |    456

as code 456 appears twice for ID 2.

The query I have been using (which is not returning the desired results is:

select id, code from table gro开发者_如何学编程up by code having count(code) > 1;

Note: the query above would return 456 and 789 as they both appear more than once but I only want it to show records for multiple appearances for a single ID.


need to group by both columns

SELECT id, code 
FROM table 
GROUP BY id, code 
HAVING COUNT(code) > 1;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜