SQL:排除根据某些列安排重复的行

发布于 2025-01-28 17:51:15 字数 411 浏览 3 评论 0 原文

在我的问题中,我有这些行:例如:

    id1  |name1| id2 | name2| c 
1   10994,Apple,22265,Banana,103
2   22265,Banana,10994,Apple,103
3   20945,Coconut,20391,Date,101
4   20391,Date,20945,Coconut,101

它们与另一列 c 一起显示ID和名称的配对组合。

考虑到ID和名称的配对,我认为第1+2和3+4是重复的。行1+2或3+4基本上显示相同的信息。

我没有运气通过分组删除重复项,因为ID1 + ID2或NAME1 + NAME2分别是不同的列。

我可以在我的SQL查询中添加一些内容以删除这些“重复项”,以便输出1+3行?

In my problem, I have these rows e.g.:

    id1  |name1| id2 | name2| c 
1   10994,Apple,22265,Banana,103
2   22265,Banana,10994,Apple,103
3   20945,Coconut,20391,Date,101
4   20391,Date,20945,Coconut,101

They show pair-wise combinations of ids and names, together with another column c.

I consider row 1+2, and 3+4 as duplicates, considering the pairings of ids and names. Rows 1+2, or 3+4 show basically the same information.

I had no luck removing the duplicates with grouping, because id1 + id2, or name1 + name2 respectively are distinct columns.

Can I add something to my SQL query that removes these 'duplicates', so that only rows 1+3 are output?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

無心 2025-02-04 17:51:15

这里的一种方法将通过 c 列进行汇总,然后使用最小/最出色的技巧来分开重复项。这是一个通用解决方案,该解决方案应在任何版本的SQL:

SELECT DISTINCT c, CASE WHEN id1 < id2 THEN id1 ELSE id2 END AS id1,
                   CASE WHEN id1 < id2 THEN id2 ELSE id1 END AS id2,
                   CASE WHEN name1 < name2 THEN name1 ELSE name2 END AS name1,
                   CASE WHEN name1 < name2 THEN name2 ELSE name1 END AS name2
FROM yourTable;

这是一个工作

请注意,在具有最少最大功能的数据库中,可以简化以下内容:

SELECT DISTINCT c, LEAST(id1, id2) AS id1,
                   GREATEST(id1, id2) AS id2,
                   LEAST(name1, name2) AS name1,
                   GREATEST(name1, name2) AS name2
FROM yourTable;

One approach here would to be aggregate by the c column and then use a least/greatest trick to tease apart the duplicates. Here is a general solution which should work on any version of SQL:

SELECT DISTINCT c, CASE WHEN id1 < id2 THEN id1 ELSE id2 END AS id1,
                   CASE WHEN id1 < id2 THEN id2 ELSE id1 END AS id2,
                   CASE WHEN name1 < name2 THEN name1 ELSE name2 END AS name1,
                   CASE WHEN name1 < name2 THEN name2 ELSE name1 END AS name2
FROM yourTable;

Here is a working demo.

Note that on databases which have a LEAST and GREATEST function, the above can be simplified to:

SELECT DISTINCT c, LEAST(id1, id2) AS id1,
                   GREATEST(id1, id2) AS id2,
                   LEAST(name1, name2) AS name1,
                   GREATEST(name1, name2) AS name2
FROM yourTable;
幸福丶如此 2025-02-04 17:51:15

您可以尝试使用不存在。您没有提供任何现有查询,而是类似的查询:

select *
from Tablename t
where not exists (
  select * from Tablename t2
  where t2.id2 = t.id1
);

You could try using not exists. You have not provided any existing query but something like:

select *
from Tablename t
where not exists (
  select * from Tablename t2
  where t2.id2 = t.id1
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文