2016-10-01 123 views
0
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。Oracle案例陳述

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); 

我收到一個錯誤,說單行子查詢返回多行。有人能告訴我我做錯了什麼嗎?

+0

您的子查詢對QDEL中的每一行都執行一次計算並返回每行一個結果。你想要一個SUM(...)在括號裏有CASE表達式。如需更多幫助,請編輯您的文章並保留數據庫的標籤(除非您同時使用SQL Server和Oracle)。 – mathguy

+0

所以我會需要案件(總和(案件.....))? – lawtonhself

+0

我刪除了'sql server'標籤,我不認爲這是有意的,因爲您清楚這是一個Oracle數據庫。 – sstan

回答

0

對於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) 
+0

1其他-1用於什麼? – lawtonhself

+0

與「delqty」相乘時,它使值爲正值或負值,這實際上是另一種向QtyOnHand加上或減去值的方法。 – sstan

+0

而且當我按原樣運行時,我仍然得到空值。我需要爲101,103,104和106添加一個特定的案例,而不是其他的-1 – lawtonhself