我希望在列中显示多次出现相同值的记录,但前提是相应的列与原始列的值不同。
例如:
Name ID
NORTH 5762
NORTH 5762
EAST 7457
WEST 1234
WEST 1234
WEST 5678
我只需要看到WEST,因为只有1次出现EAST,虽然有多次出现在北方,但它们都共享相同的ID值。
答案 0 :(得分:2)
SELECT a.*
FROM tableName a
WHERE EXISTS
(
SELECT 1
FROM TableName b
WHERE a.Name = b.name
GROUP BY NAME
HAVING COUNT(DISTINCT ID) > 1 AND
COUNT(*) > 1
)
输出
╔══════╦══════╗
║ NAME ║ ID ║
╠══════╬══════╣
║ WEST ║ 1234 ║
║ WEST ║ 1234 ║
║ WEST ║ 5678 ║
╚══════╩══════╝
或者如果您不想查看所有记录,
SELECT Name
FROM TableName
GROUP BY NAME
HAVING COUNT(DISTINCT ID) > 1 AND COUNT(*) > 1
输出
╔══════╗
║ NAME ║
╠══════╣
║ WEST ║
╚══════╝
答案 1 :(得分:0)
所以你想按姓名和身份分组,只有1个结果 - 你这样做:
SELECT Name
FROM Table
WHERE Name IN
(
SELECT Name
FROM Table
GROUP BY Name, ID
HAVING COUNT(*) = 1
)
GROUP BY NAME
HAVING COUNT(NAME) > 1