以下是我需要完成的更好理解的粗略示例:
SampleTable
PrimaryKey SpecialCol1 RandomCol SpecialCol2
1 aaa sample1 111
2 aaa sample2 222
3 bbb sample3 444
4 aaa sample4 111
5 ccc sample5 444
6 bbb sample6 444
最终结果应如下所示:
PrimaryKey SpecialCol1 RandomCol SpecialCol2
--------------------------------------------
2 aaa sample2 222
5 ccc sample5 444
所以我只需要选择那些SpecialCol1和SpecialCol2组合在该表中唯一的行。我花了很多时间试图弄清楚它是如何完成的,但到目前为止还没有运气。
答案 0 :(得分:3)
SELECT MIN(primarykey), specialcol1, MIN(randomcol), specialcol2
FROM sampletable
GROUP BY specialcol1, specialcol2
HAVING COUNT(*) = 1;
答案 1 :(得分:1)
分阶段进行。
查找列组合唯一的行:
SELECT SpecialCol1, SpecialCol2
FROM SampleTable -- Thank you for invcluding the tablename
GROUP BY SpecialCol1, SpecialCol2
HAVING COUNT(*) = 1;
查找匹配行的其他详细信息:
SELECT s.*
FROM SampleTable AS s
JOIN (SELECT SpecialCol1, SpecialCol2
FROM SampleTable -- Thank you for invcluding the tablename
GROUP BY SpecialCol1, SpecialCol2
HAVING COUNT(*) = 1
) AS t
ON t.SpecialCol1 = s.SpecialCol1 AND t.SpecialCol2 = s.SpecialCol2;
答案 2 :(得分:0)
试试这个
SELECT PrimaryKey
,SpecialCol1
,RandomCol
,SpecialCol2
FROM (( SELECT SpecialCol1+SpecialCol2 AS test
,PrimaryKey
,SpecialCol1
,RandomCol
,SpecialCol2
FROM table)) AS temp
WHERE test IN (SELECT test
FROM ( SELECT SpecialCol1+SpecialCol2 AS test
FROM table) AS temp
GROUP BY test
HAVING COUNT(test)=1 )