2013-10-02 34 views
1

我有兩個表 - table1和table2。這兩個表的當前條件如下:更新SQL Server 2008中源表中多個匹配行的查詢?

表1:

id type mon tue wed thu fri sat sun 
1 ets NULL NULL NULL NULL NULL NULL NULL 
1 ets NULL NULL NULL NULL NULL NULL NULL 
1 eta NULL NULL NULL NULL NULL NULL NULL 
1 eta NULL NULL NULL NULL NULL NULL NULL 
1 cl NULL NULL NULL NULL NULL NULL NULL 
1 cl NULL NULL NULL NULL NULL NULL NULL 
2 ets NULL NULL NULL NULL NULL NULL NULL 
2 ets NULL NULL NULL NULL NULL NULL NULL 
2 eta NULL NULL NULL NULL NULL NULL NULL 
2 eta NULL NULL NULL NULL NULL NULL NULL 
2 cl NULL NULL NULL NULL NULL NULL NULL 
2 cl NULL NULL NULL NULL NULL NULL NULL 

表2:

id ets eta cl 
1 mon tue wed 
1 thu fri sat 
1 sun mon tue 
2 sat sun mon 
2 fri sat sun 

注意,各列的表2中的名稱在表1的列「類型」。 我想更新table1,使列mon-sun得到table2中各個'type'(即ets,eta或cl)中找到的所有值的更新,而table1.id應該匹配table2.id。

所得表我想是象下面對上述數據:

id type mon tue wed thu fri sat sun 
1 ets 1  NULL NULL 1  NULL NULL 1 
1 ets 1  NULL NULL 1  NULL NULL 1 
1 eta 1  1  NULL NULL 1  NULL NULL 
1 eta 1  1  NULL NULL 1  NULL NULL 
1 cl NULL 1  1  NULL NULL 1  NULL 
1 cl NULL 1  1  NULL NULL 1  NULL 
2 ets NULL NULL NULL NULL 1  1  NULL 
2 ets NULL NULL NULL NULL 1  1  NULL 
2 eta NULL NULL NULL NULL NULL 1  1 
2 eta NULL NULL NULL NULL NULL 1  1 
2 cl 1  NULL NULL NULL NULL NULL 1 
2 cl 1  NULL NULL NULL NULL NULL 1 

的UPDATE查詢我申請是如下:

update a 
set a.mon = case b.ets when 'mon' then '1' else '0' end, 
a.tue = case b.ets when 'tue' then '1' else '0' end, 
a.wed = case b.ets when 'wed' then '1' else '0' end, 
a.thu = case b.ets when 'thu' then '1' else '0' end, 
a.fri = case b.ets when 'fri' then '1' else '0' end, 
a.sat = case b.ets when 'sat' then '1' else '0' end, 
a.sun = case b.ets when 'sun' then '1' else '0' end 
from table1 a, table2 b 
where a.id = b.id and a.type = 'ets' 

考慮第一更新表table1.type = 'ets'和的各自的值來自table2的id與ID同時匹配。

上述查詢僅從table2中獲取第一個匹配值,並更新table1中的值,而不是其他值。

任何幫助將不勝感激。

+0

什麼地方a.rownum = b.rownum?沒有這樣的專欄'rownum'。 –

+0

它是同一個colum id。我錯誤地鍵入rownum istead of id。 –

+0

在表1中所有行都是重複的。爲什麼? –

回答

2

你要 「轉動」 你的表更新前:

with cte as (
    select 
     t.id, c.type, 
     max(case when c.value = 'mon' then 1 end) as mon, 
     max(case when c.value = 'tue' then 1 end) as tue, 
     max(case when c.value = 'wed' then 1 end) as wed, 
     max(case when c.value = 'thu' then 1 end) as thu, 
     max(case when c.value = 'fri' then 1 end) as fri, 
     max(case when c.value = 'sat' then 1 end) as sat, 
     max(case when c.value = 'sun' then 1 end) as sun 
    from Table2 as t 
     outer apply (values 
      ('ets', t.[ets]), 
      ('eta', t.[eta]), 
      ('cl', t.[cl]) 
     ) as c(type, value) 
    group by t.id, c.type 
) 
update Table1 set 
    mon = c.mon, 
    tue = c.tue, 
    wed = c.wed, 
    thu = c.thu, 
    fri = c.fri, 
    sat = c.sat, 
    sun = c.sun 
from Table1 as t 
    inner join cte as c on c.id = t.id and c.type = t.type 

sql fiddle demo

另一種方式來做到這一點是旋轉/逆透視:

with cte_up as (
    select id, 1 as value, type, name 
    from Table2 
    unpivot (name for type in ([ets],[eta],[cl])) as up 
), cte_p as (
    select * 
    from cte_up 
    pivot (max(value) for name in ([mon], [tue], [wed], [thu], [fri], [sat], [sun])) as p 
) 
update Table1 set 
    mon = c.mon, 
    tue = c.tue, 
    wed = c.wed, 
    thu = c.thu, 
    fri = c.fri, 
    sat = c.sat, 
    sun = c.sun 
from Table1 as t 
    inner join cte_p as c on c.id = t.id and c.type = t.type 

sql fiddle demo

+0

很好解決! +1 – MrSimpleMind

+0

太棒了。非常感謝你羅曼·派克。不得不努力學習「支點」。 –

+0

太棒了。第二個解決方案也很好。感謝您的幫助。 :) –