2012-04-17 70 views
2

问题:如何更新表与使用内部连接和Case语句

表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 

结果:更新表2表1像

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

结果应该如何来:

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

​​

我想减去像表2(各O/N/U /其他)=表2(各O/N/U /其他) - 表1(类型)和宁愿溶液不循环

我试过,但不能正常工作 -

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 

回答

3

试试这个

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
文档
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表2,你会做以下操作:对于PIVOT使用

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 
+0

+1 – wickedone 2012-04-17 16:37:06