如何比较两个表并选择顺序中的差异?

时间:2017-01-09 11:00:00

标签: sql sql-server

我有两张几乎完全相同的表,因为某些行的顺序在两个表之间混合了。我将举例说明我的表格如何:

table 1

colum1 | colum2 | colum3
-------+--------+-----------
test1  | pizza  | margarita 
test1  | pizza  | cheese    
test1  | pizza  | hawaii    
test1  | burger | salad     
test2  | pizza  | margarita 
test2  | pizza  | ham       
test2  | burger | tomat     
test2  | burger | salad     
test3  | pig    | green     
test3  | cow    | green     
test3  | cow    | yellow    
test3  | pig    | bird      

table 2

colum1 | colum2 | colum3
-------+--------+------------
test1  | pizza  | margarita 
test1  | pizza  | hawaii      <-- different spot than before
test1  | pizza  | cheese      <-- different spot than before 
test1  | burger | salad     
test2  | pizza  | margarita 
test2  | pizza  | ham       
------ | ------ | ---------  <-- no value between those (so its missing)
test2  | burger | salad     
test3  | pig    | green     
test3  | cow    | green     
test3  | cow    | yellow    
test3  | pig    | bird      

正如您在上表中看到的那样,有一行混合,并且缺少一行。现在我想进行如下查询:“如果colum1相同,那么检查行是否与另一个表的顺序相同”如果不是这样,“只要colum1具有相同的值,就显示所有行。“所以结果就是这样:

         table1                    table2
------+-------+-----------+-------+-------+-----------
test1 | pizza | margarita | test1 | pizza | margarita 
test1 | pizza | cheese    | test1 | pizza | hawaii        <<<shows all `test1` because this one is different
test1 | pizza | hawaii    | test1 | pizza | cheese    
test1 | burger| salad     | test1 | pizza | salad     
test2 | pizza | margarita | test2 | pizza | margarita 
test2 | pizza | ham       | test2 | pizza | ham       
test2 | burger| tomat     |       |       |               <<<shows all `test2` because this one is different
test2 | burger| salad     | test2 | burger| salad      

另外:一行(所以colum1,colum2,colum3组合)总是相同的。

那么,这在某种程度上可能与SQL有关吗?或者我应该使用别的东西来做这件事吗?

2 个答案:

答案 0 :(得分:0)

左/右连接带有排序和row_number函数的表,其中包含您要比较的所有列以及有序数字,并且表将有不同的空值。

答案 1 :(得分:0)

  

注意:你需要有一个索引,所以生成它的错误ID是使用这个代码:

/etc/php5/apache2/php.ini

但是如果你想跟踪这些表之间的变化,你可以使用这样的查询:

row_number() over (order by (select 0)) as rowId

结果如下:

;with t1 as (
    select *, row_number() over (order by (select 0)) as rowId
    from table1
), t2 as (
    select *, row_number() over (order by (select 0)) as rowId
    from table2
), t as (
select t1.rowId, t1.colum1, t1.colum2, t1.colum3
    , coalesce(t2.rowId - t1.rowId, 999999999) + sum(case when t2.rowId is null then 1 else 0 end) over (order by t1.rowId) cs
from t1 full outer join t2 
  on t1.colum1 = t2.colum1 and t1.colum2 = t2.colum2 and t1.colum3 = t2.colum3 
)
select t.colum1, t.colum2, t.colum3,
    case when t.cs >= 999999999 then 'skipped'
         when t.cs = 0 then ''
         else 'replaced'
    end table2State
from t
order by t.rowId;