簡體   English   中英

更新查詢從SQL Server 2008年源表中的多個匹配的行嗎?

[英]update query for more than one matched rows from source table in SQL server 2008?

我有兩個表-table1和table2。 這兩個表的當前條件如下:

表格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中為各個“類型”(即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'第一個更新表,以及同時匹配id的來自table2 table1.type = 'ets'各個值。

上面的查詢僅采用table2中的第一個匹配值,而不更新其余的table1中的值。

任何幫助將不勝感激。

您必須先“旋轉”表才能更新:

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小提琴演示

做到這一點的另一種方法是樞軸/取消樞軸:

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小提琴演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM