簡體   English   中英

Oracle案例陳述

[英]Oracle case statement

If I have 2 tables:

table1-QITEM
ITEMNAME                              QTYONHAND
---------------------------------------------
boots-snakeproof                      100
camel saddle                          100
compass                               100
elephant polo stick                   100
exploring in 10 easy lessons          100
geo positioning system                100
hammock                               100
hat-polar explorer                    100
how to win foreign friends            100
map case                              100
map measure                           100
pith helmet                           100
pocket knife-avon                     100
pocket knife-nile                     100
safari chair                          100
safari cooking kit                    100
sextant                               100
stetson                               100
tent-2 person                         100
tent-8 person                         100


table2-QDEL
DELNO     DELQTY   ITEMNAME                         SPL 
-------------------------------------------------------
51        50       pocket knife-nile                102
52        10       pocket knife-nile                105
53        10       pocket knife-nile                105
54        10       pocket knife-nile                105
55        10       pocket knife-nile                105
56        10       pocket knife-nile                105
57        50       compass                          101
58        10       geo positioning system           101
59        10       map measure                      101
60        25       map case                         101
61        2        sextant                          101
62        1        sextant                          105
63        20       compass                          103
64        1        geo positioning system           103
65        15       map measure                      103
66        1        sextant                          103
67        5        sextant                          102
68        3        sextant                          104
69        5        boots-snakeproof                 105
70        15       pith helmet                      105
71        1        pith helmet                      101
72        1        pith helmet                      102
73        1        pith helmet                      103
74        1        pith helmet                      104
75        5        pith helmet                      105
76        5        pith helmet                      105
77        5        pith helmet                      105
78        5        pith helmet                      105
79        5        pith helmet                      105
80        10       pocket knife-nile                102
81        1        compass                          102
82        1        geo positioning system           102
83        10       map measure                      102
84        5        map case                         102
85        5        compass                          102
86        5        pocket knife-avon                102
87        5        tent-2 person                    102
88        2        tent-8 person                    102
89        5        exploring in 10 easy lessons     102
90        5        how to win foreign friends       102
91        10       exploring in 10 easy lessons     102
92        10       how to win foreign friends       102
93        2        exploring in 10 easy lessons     102
94        2        how to win foreign friends       102
95        5        compass                          105
96        2        boots-snakeproof                 105
97        20       pith helmet                      106
98        20       pocket knife-nile                106
99        1        sextant                          106
100       3        hat-polar explorer               105
101       3        stetson                          105

我正在嘗試使用QDEL的購買/銷售來更新QITEM。 購買是在SPL = 102或105時進行的。因此,您將在spl = 102或105時添加數量。在spl =其他值時,您將減去數量。 您要從QDEL中增加或減去DELQTY中的金額,並將其放入QITEM中的QTYONHAND中。 我無法使我的代碼正常工作。 我正在使用Oracle Developer btw。

update QITEM i
set i.QtyOnHand = (select case when x.SPLNO = 101 then i.QtyOnHand -        x.DELQTY
                      when x.SPLNO = 102 then i.QtyOnHand + x.DELQTY
                      when x.SPLNO = 103 then i.QtyOnHand - x.DELQTY
                      when x.SPLNO = 104 then i.QtyOnHand - x.DELQTY
                      when x.SPLNO = 105 then i.QtyOnHand + x.DELQTY
                          else i.QtyOnHand - x.DELQTY end
                from QDEL x
               where x.ITEMNAME = i.ITEMNAME);

我收到一條錯誤消息,說單行子查詢返回多個行。 有人可以告訴我我在做什么錯嗎?

對於每一行QITEM ,你的子查詢必須返回一個值來更新QtyOnHand列。 您的子查詢當前針對給定的itemname返回多個值。 也許您以某種方式期望它可以神奇地為您執行某種循環,但這種方式行不通。

正如評論所說,你需要使用sum聚集功能,保證你從子查詢單個值。 這是一種實現方法:

update qitem i
   set i.QtyOnHand = 
           i.QtyOnHand + (select coalesce(sum(x.delqty * case when x.splno in (102, 105) then 1 else -1 end), 0)
                            from qdel x
                           where x.itemname = i.itemname)

暫無
暫無

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

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