问题
I have a table as such
custno, type, color
A1234, B, Red
A1234, C, Blue
A1277, B, Red
A1288, A, Black
A1288, B, Red
A1289, A, Black
I need to retrieve only the unique records A1277 and A1289 that are only found once.
回答1:
This will display the custNO on the result list,
SELECT custNo
FROM tableName
GROUP BY custNO
HAVING COUNT(*) = 1
but if you want to get the whole row,
SELECT a.*
FROM tableName a
INNER JOIN
(
SELECT custNo
FROM tableName
GROUP BY custNO
HAVING COUNT(*) = 1
) b ON a.custNo = b.custNo
- SQLFiddle Demo (both queries)
回答2:
SELECT custno
FROM yourTable
GROUP BY custno
HAVING COUNT(*) = 1
Example Fiddle
回答3:
Try this as this will prevent costly join operation--
SELECT custno,type,color FROM
(
SELECT custno, type, color, COUNT(custno) OVER(PARTITION BY custno) CNT
FROM tableName
)TMP WHERE CNT=1
来源:https://stackoverflow.com/questions/15364846/select-individual-records-only