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

結果: 表 2 を表 1 のように更新します。

-------------------------------------------------
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/Other) = table2(それぞれの 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

何が問題でしたか:

  • with joins では、Updateエイリアスを使用して更新テーブルを指定します (t2この場合)。TSQL Update ステートメントのドキュメントを参照してください。
  • Old = Old - [...]行が重複していました -Useless = [...]代わりに行を入れました
  • The CASEsyntax was wrong: ( CASE <var> WHEN <value> [...]is wrong; is correct) TSQL CASE ステートメントCASE WHEN <condition> THEN <value> [...]のドキュメントを参照してください
于 2012-04-17T15:23:21.207 に答える
3

このようにできます。最初に a を使用しPIVOTて 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 に答える