我有一个表File_Details
,有些记录是重复的。如何找到任何列中具有不同值但其余列相似的记录?
File_Details
:
-----------------------
| Code | Name |
-----------------------
| 123 | text1.txt |
| 123 | text1.txt |
| 124 | text4.txt |
| 124 | text5.txt |
| 125 | text6.txt |
| 125 | text6.txt |
-----------------------
我需要列出代码124,因为代码相同但Name
不同
答案 0 :(得分:1)
DECLARE @TABLE TABLE (code INT, Name VARCHAR(100))
INSERT INTO @TABLE VALUES
(123 ,'text1.txt'),
(123 ,'text1.txt'),
(124 ,'text4.txt'),
(124 ,'text5.txt'),
(125 ,'text6.txt'),
(125 ,'text6.txt')
SELECT *
FROM @TABLE t
WHERE EXISTS (SELECT 1
FROM @TABLE
WHERE code = t.code
AND Name <> t.Name)
╔══════╦═══════════╗
║ code ║ Name ║
╠══════╬═══════════╣
║ 124 ║ text4.txt ║
║ 124 ║ text5.txt ║
╚══════╩═══════════╝
答案 1 :(得分:0)
要查找任何代码/名称组合,而不是完全重复:
SELECT Code, Name, COUNT(1)
FROM File_Details
GROUP BY Code, Name
HAVING COUNT(1) <> 2
答案 2 :(得分:0)
选择包含非唯一&#39;代码&#39;但唯一&#39;代码&#39;
的记录SELECT CODE , NAME
FROM file_Details
WHERE CODE in (SELECT CODE
FROM file_Details
GROUP BY CODE HAVING COUNT (*) > 1)
AND NAME IN (SELECT NAME
FROM file_Details
GROUP BY NAME HAVING COUNT (*) = 1)