匹配两个数据集中列的排列

时间:2013-11-18 18:48:03

标签: sql database algorithm csv permutation

我有两组数据(csv文件),包含密钥数据库列的出现次数和出现的次数。 (原始数据库表不再可用)

例如:

设置1

column 1  column 2  column 3  count
1         2         3         100
1         2         2         200
3         1         1         700

设置2

column 1  column 2  column 3  count
1         2         3         500
1         2         2         133
1         1         1         100

我需要一些比较两个表的方法来告诉我第1组中是否存在组合2中不存在的组合。

我正在考虑编写一个脚本,它将使用第2组的嵌套循环遍历set one,但我想知道是否有更好的方法来执行此操作。

愿意接受建议吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

请不要循环:)

select t1.* 
from t1 left join t2 on t1.col1 = t2.col1 and t1.col2 = t2.col2 and t1.col3 = t2.col3
where t2.col1 is null

左连接创建没有找到记录的空值。左连接t1到t2以及t2为空的所有时间表示t1中的记录在t2中没有匹配。

将此更改为右连接,并且t1.col1为空,您将看到所有t2也不在t1中。

答案 1 :(得分:1)

您没有指定DBMS,因此这是ANSI SQL:

获取table2中不在table1中的组合:

select col1, col2, col3
from table_1
except
select col1, col2, col3
from table_2;

获取table1中不在table2中的组合:

select col1, col2, col3
from table_2
except 
select col1, col2, col3
from table_1;

在一个陈述中得到两者:

(
  select col1, col2, col3
  from table_1
  except
  select col1, col2, col3
  from table_2
)
union all
(
  select col1, col2, col3
  from table_2
  except
  select col1, col2, col3
  from table_1
);

SQLFiddle示例:http://sqlfiddle.com/#!15/71566/1

相关问题