2

问题 :

表格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)

我想减去 Like table2(Respective O/N/U/Other) = table2(Respective O/N/U/Other) - table1(Type) 并且更喜欢没有循环的解决方案

我试过了,但不能正常工作——

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
4

2 回答 2

5

尝试这个

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

什么问题:

  • Updatewith 连接中,使用别名(t2在我们的例子中)指定更新表。请参阅TSQL 更新语句的文档
  • Old = Old - [...]行被重复 - 我Useless = [...]改为放置该行
  • CASE语法错误:(是错误的CASE <var> WHEN <value> [...]CASE WHEN <condition> THEN <value> [...]是正确的)请参阅TSQL CASE 语句的文档
于 2012-04-17T15:23:21.207 回答
3

你可以这样做。我首先使用 aPIVOT来更改 Table1 的设置,然后对 table2 执行 JOIN。这为您提供了您想要的结果。这将向您显示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及其工作的演示。

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

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
于 2012-04-17T15:36:32.797 回答