仅选择单个记录

时间:2013-03-12 15:05:39

标签: sql select db2

我有一张桌子

custno, type, color
A1234, B, Red
A1234, C, Blue
A1277, B, Red
A1288, A, Black
A1288, B, Red
A1289, A, Black

我只需要检索只能找到一次的唯一记录A1277和A1289。

3 个答案:

答案 0 :(得分:2)

这将在结果列表中显示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

答案 1 :(得分:1)

SELECT custno
FROM yourTable
GROUP BY custno
HAVING COUNT(*) = 1

Example Fiddle

答案 2 :(得分:0)

尝试这样做可以防止代价高昂的加入操作 -

SELECT custno,type,color FROM ( SELECT custno, type, color, COUNT(custno) OVER(PARTITION BY custno) CNT FROM tableName )TMP WHERE CNT=1