我有一张这样的桌子
custno, type, color
A1234, B, Red
A1234, C, Blue
A1277, B, Red
A1288, A, Black
A1288, B, Red
A1289, A, Black
我只需要检索仅找到一次的唯一记录 A1277 和 A1289。
这将显示custNO
在结果列表中,
SELECT custNo
FROM tableName
GROUP BY custNO
HAVING COUNT(*) = 1
但如果你想得到整行,
SELECT a.*
FROM tableName a
INNER JOIN
(
SELECT custNo
FROM tableName
GROUP BY custNO
HAVING COUNT(*) = 1
) b ON a.custNo = b.custNo
SELECT custno
FROM yourTable
GROUP BY custno
HAVING COUNT(*) = 1
试试这个,因为这样可以防止代价高昂的连接操作——
SELECT custno,type,color FROM
(
SELECT custno, type, color, COUNT(custno) OVER(PARTITION BY custno) CNT
FROM tableName
)TMP WHERE CNT=1