对于数据:
ID Name Code
---------------------------
1 A X
2 B XX
3 A X
4 B XX
5 C XXX
希望通过一个SQL语句过滤出形如:
1 A X
2 B XX
的数据。
可使用至少两种方式来做:
方法一:
SELECT CODE,NAME,COUNT(*) AS [COUNT],MAX(ID) AS IDFROM TESTGROUP BY CODE,NAME HAVING COUNT(*) > 1
方法二:
SELECT * FROM (SELECT NAME,CODE,(SELECT COUNT(*) FROM TEST WHERE NAME=A.NAME AND CODE=A.CODE ) AS [COUNT],MAX(ID) AS IDFROM TEST AGROUP BY NAME,CODE) RESWHERE [COUNT] > 1