2017-01-09 86 views
0

我得到了兩張幾乎相同的表格,這兩張表格之間的某些行的順序是混合在一起的。我會給你一個例子,說明我的表看起來像:如何比較兩個表並選擇順序中的差異?

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的某種可能嗎?或者我應該使用其他方法來做到這一點?

+0

這是什麼意思*訂單*在這裏? –

+3

表格數據是無序的。 – jarlh

+0

這是一個簡單的有序完整外部聯接,其中過濾掉第二個表格列中的一個空值。獎金:這是沒有意義的「如果一列相同,然後檢查所有列是相同的,簡單去」所有列都是一樣的 –

回答

0

注意:您需要有一個索引,一個壞的ID生成它使用此代碼:

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; 

這導致這樣的:

colum1 | colum2 | colum3 | table2State 
--------+-----------+-----------+-------------- 
test1 | pizza  | margarita | 
test1 | pizza  | cheese | replaced 
test1 | pizza  | hawaii | replaced 
test1 | burger | salad  | 
test2 | pizza  | margarita | 
test2 | pizza  | ham  | 
test2 | burger | tomat  | skipped 
test2 | burger | salad  | 
test3 | pig  | green  | 
+0

這幾乎可行,當我用'test3 | pig | bird'切換'test3 | pig | green'時,它會給出正確的答案。 e只有在值低於或低於本身時才檢測到開關。當colum1具有相同的值時,可以在這些值中切換。因此'test1'和'test2'和'test3'之間的值可以切換。但他們不能在這些之外切換。所以在這個例子中'pizza |瑪格麗塔「可以切換到其他3個地方 –

0

將帶有排序和row_number函數的表格與所有希望比較的列加上排序的數字進行左右連接,並且在表格不同的位置會出現空值。

+0

但是,該順序差異會丟失。 – jarlh

+0

您可以通過以下方式添加訂單: (select * from t1 order by 1)x left join(select * from t2 order by 1)y on ... – Dexion

+0

但這不是OP所要求的 – jarlh