简体   繁体   English

SQL-以混杂的顺序将相似的记录与相同的一组值匹配

[英]SQL - matching similar records with same set of values in jumbled order

Say I have a table with the following values in three columns 假设我有一个表格,其中三列中包含以下值

John Smith, Edward Jones, 4  
John Deer, Jane Deer, 2  
Edward Jones, John Smith, 4

I would like to have a query that recognizes the first and third records as similar, selecting only the first and second records and leaving the third out. 我想要一个将第一条记录和第三条记录识别为相似的查询,仅选择第一条记录和第二条记录,而忽略第三条记录。 I can easily do this when the values are in the same order, but I'm having trouble coming up with something that can discern similarity when the values are in arbitrary order like this. 当值以相同的顺序排列时,我可以轻松地做到这一点,但是当这样的值以任意顺序排列时,我很难想到可以分辨相似性的东西。 Any ideas? 有任何想法吗?

[edited for clarity - this is a table with three columns] [为清楚起见而编辑-这是一个包含三列的表格]

You should be able to use case statements to put the name columns into a consistent order. 您应该能够使用case语句将名称列置于一致的顺序。 Something like 就像是

create table n( n1 varchar(20), n2 varchar(20), v int);

insert into n select "john", "edward",4;
insert into n select "john", "jane", 3;
insert into n select "edward","john",4;



select distinct
  case when n1>n2 then n1 else n2 end n1,
  case when n1>n2 then n2 else n1 end n2,
  v
from n

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM