删除两个不同列之间的重复行
Remove duplicate rows between two different columns
我在删除两个不同列之间的重复项时遇到问题。这是我当前的 select 查询:
SELECT e.id AS EventId,
rt.player_nbr AS PlayerId,
rt.partner_nbr AS PartnerId
FROM eventTable e
INNER JOIN resultsTable rt ON e.id = rt.id
结果如下:
EventId | PlayerId | PartnerId
1049 | 20652 | 80458
1049 | 20702 | 310816
1049 | 80394 | 190666
1049 | 80394 | null
1049 | 80458 | 20652
1049 | 190666 | 80394
从结果中可以看到,PlayerId 和PartnerId 都被打乱了。我想将重复的行删除到如下所示的位置:
EventId | PlayerId | PartnerId
1049 | 20652 | 80458
1049 | 20702 | 310816
1049 | 80394 | 190666
1049 | 80394 | null
到目前为止,我已经尝试过这个和其他一些东西但没有成功:
SELECT e.id AS EventId,
rt.player_nbr AS PlayerId,
rt.partner_nbr AS PartnerId
FROM eventTable e
INNER JOIN resultsTable rt ON e.id = rt.id
WHERE NOT EXISTS(
SELECT *
FROM eventTable e2
INNER JOIN resultsTable rt2 ON e2.id = rt2.id
WHERE rt2.player_nbr = rt.partner_nbr
AND rt2.partner_nbr = rt.player_nbr
)
关于我如何最好地处理这个问题有什么建议吗?提前致谢。
您的示例查询使用 not exists
,所以让我们继续沿着这条路走下去。逻辑是您希望所有行都是 player_nbr < partner_nbr
。然后你想要 player_nbr > partner_nbr
为真但有其他行 player_nbr < partner_nbr
的行。以下是SQL中的逻辑:
SELECT e.id AS EventId,
rt.player_nbr AS PlayerId,
rt.partner_nbr AS PartnerId
FROM eventTable e INNER JOIN
resultsTable rt
ON e.id = rt.id
WHERE rt.player_nbr < rt.partner_nbr OR
NOT EXISTS (SELECT 1
FROM resultsTable rt2
WHERE rt2.id = rt.id AND
rt2.player_nbr = rt.partner_nbr AND
rt2.partner_nbr = rt.player_nbr
);
我在删除两个不同列之间的重复项时遇到问题。这是我当前的 select 查询:
SELECT e.id AS EventId,
rt.player_nbr AS PlayerId,
rt.partner_nbr AS PartnerId
FROM eventTable e
INNER JOIN resultsTable rt ON e.id = rt.id
结果如下:
EventId | PlayerId | PartnerId
1049 | 20652 | 80458
1049 | 20702 | 310816
1049 | 80394 | 190666
1049 | 80394 | null
1049 | 80458 | 20652
1049 | 190666 | 80394
从结果中可以看到,PlayerId 和PartnerId 都被打乱了。我想将重复的行删除到如下所示的位置:
EventId | PlayerId | PartnerId
1049 | 20652 | 80458
1049 | 20702 | 310816
1049 | 80394 | 190666
1049 | 80394 | null
到目前为止,我已经尝试过这个和其他一些东西但没有成功:
SELECT e.id AS EventId,
rt.player_nbr AS PlayerId,
rt.partner_nbr AS PartnerId
FROM eventTable e
INNER JOIN resultsTable rt ON e.id = rt.id
WHERE NOT EXISTS(
SELECT *
FROM eventTable e2
INNER JOIN resultsTable rt2 ON e2.id = rt2.id
WHERE rt2.player_nbr = rt.partner_nbr
AND rt2.partner_nbr = rt.player_nbr
)
关于我如何最好地处理这个问题有什么建议吗?提前致谢。
您的示例查询使用 not exists
,所以让我们继续沿着这条路走下去。逻辑是您希望所有行都是 player_nbr < partner_nbr
。然后你想要 player_nbr > partner_nbr
为真但有其他行 player_nbr < partner_nbr
的行。以下是SQL中的逻辑:
SELECT e.id AS EventId,
rt.player_nbr AS PlayerId,
rt.partner_nbr AS PartnerId
FROM eventTable e INNER JOIN
resultsTable rt
ON e.id = rt.id
WHERE rt.player_nbr < rt.partner_nbr OR
NOT EXISTS (SELECT 1
FROM resultsTable rt2
WHERE rt2.id = rt.id AND
rt2.player_nbr = rt.partner_nbr AND
rt2.partner_nbr = rt.player_nbr
);