Oracle案例陈述
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);
我收到一个错误,说单行子查询返回多行。有人能告诉我我做错了什么吗?
对于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)
1其他-1用于什么? – lawtonhself
与“delqty”相乘时,它使值为正值或负值,这实际上是另一种向QtyOnHand加上或减去值的方法。 – sstan
而且当我按原样运行时,我仍然得到空值。我需要为101,103,104和106添加一个特定的案例,而不是其他的-1 – lawtonhself
您的子查询对QDEL中的每一行都执行一次计算并返回每行一个结果。你想要一个SUM(...)在括号里有CASE表达式。如需更多帮助,请编辑您的文章并保留数据库的标签(除非您同时使用SQL Server和Oracle)。 – mathguy
所以我会需要案件(总和(案件.....))? – lawtonhself
我删除了'sql server'标签,我不认为这是有意的,因为您清楚这是一个Oracle数据库。 – sstan