如何使用内部联接和案例声明更新表

时间:2012-04-17 15:09:58

标签: sql sql-server

问题:

表1

CatId - -   Type - -    Qty
==============================
8        ||    O   ||   10
8        ||    N   ||   20
8        ||    U   ||   30
30       ||    N   ||   5
30       ||    O   ||   15
30       || NULL   ||   25

表2

catId -- Old -    -New -- Useless -- Other
========================================
8      || 100   || 70   ||  140      || 110
30     || 10    || 20   ||  30       || 50

结果:使用表1更新表2

-------------------------------------------------
catId --   Old --   New -- Useless -- Other
8       || 90    || 50  ||  110     || 110
30      ||  5    ||  5  ||  30      || 25

结果如何:

表1和表2有一个共同的列CatId。

Column of table 1 Type is connects with Table2  
    AS  (Old - O / New - N / Useless - U / Other - NULL)

我想减去像table2(各自的O / N / U /其他)= table2(各自的O / N / U /其他) - table1(类型)并且更喜欢没有循环的解决方案

我试过这个但没有正常工作 -

Update Table2
Set New = New - (CASE Type WHEN 'N' THEN (Table1.qty) Else 0 End),
    Old = Old  - (CASE Type WHEN 'O' THEN (Table1.qty) Else 0 End),
    Old = Old  - (CASE Type WHEN 'O' THEN (Table1.qty) Else 0 End),
    Other= Othere- (CASE Type WHEN is Null THEN (Table1.qty) Else 0 End)
from table1 
inner join table2 
On table1.catId = table2 .catId

2 个答案:

答案 0 :(得分:4)

试试这个

Update t2
Set New = New - (CASE WHEN type='N' THEN (t1.qty) Else 0 End),
    Old = Old  - (CASE WHEN type='O' THEN (t1.qty) Else 0 End),
    Useless = Useless  - (CASE WHEN type='U' THEN (t1.qty) Else 0 End),
    Other= Other - (CASE WHEN type is Null THEN (t1.qty) Else 0 End)
from Table1 t1
inner join Table2 t2
On t1.catId = t2.catId

出了什么问题:

    在带有联接的Update
  • ,使用别名(在我们的示例中为t2)指定更新表。请参阅TSQL Update statement
  • 的文档
  • Old = Old - [...]行重复了 - 我将Useless = [...]行改为
  • CASE语法错误:(CASE <var> WHEN <value> [...]错误; CASE WHEN <condition> THEN <value> [...]正确)请参阅the TSQL CASE statement
  • 的文档

答案 1 :(得分:3)

你可以这样做。我首先使用PIVOT来改变Table1的设置,然后将JOIN执行到table2。这将为您提供所需的结果。这将显示您想要的SELECT数据

create table #t1
(
    catid int,
    type varchar(5),
    qty int
)
create table #t2
(
    catid int,
    old int,
    new int,
    useless int,
    other int
)

insert into #t1 values(8, 'O', 10)
insert into #t1 values(8, 'N', 20)
insert into #t1 values(8, 'U', 30)
insert into #t1 values(30, 'N', 5)
insert into #t1 values(30, 'O', 15)
insert into #t1 values(30, null, 25)

insert into #t2 values(8, 100, 70, 140, 110)
insert into #t2 values(30, 10, 20, 30, 50)

select t2.catid 
    , t2.Old - t1.old as Old
    , t2.new - t1.new as New
    , t2.Useless - t1.useless as Useless
    , t2.other - t1.other as Other
from #t2 t2
INNER JOIN 
(
    select catid
        , IsNull([O], 0) as Old
        , IsNull([N], 0) as New
        , IsNull([U], 0) as useless
        , IsNull([null], 0) as other
    from
    (
        select catid, type, qty
        from #t1
    ) x
    PIVOT
    (
        max(qty)
        for type in([O], [N], [U], [null])
    ) p
) t1
    on t2.catid = t1.catid

结果:

catid   Old   New   Useless   Other
8       90    50    110       110
30      -5    15    30        50

这是一个sqlfiddle,其演示正在运行。

然后,如果您想要UPDATE table2,您将执行以下操作:

UPDATE t2
SET t2.Old = t2.Old - t1.old
    , t2.new  = t2.new - t1.new
    , t2.Useless = t2.Useless - t1.useless
    , t2.other = t2.other - t1.other
from #t2 t2
INNER JOIN 
(
    select catid
        , IsNull([O], 0) as Old
        , IsNull([N], 0) as New
        , IsNull([U], 0) as useless
        , IsNull([null], 0) as other
    from
    (
        select catid, type, qty
        from #t1
    ) x
    PIVOT
    (
        max(qty)
        for type in([O], [N], [U], [null])
    ) p
) t1
    on t2.catid = t1.catid