Table:T1 (key, numval, state) and T2(key, foreignkey:T1.key, type)
我有两个表T1和T2,每个T1都有多个值 T2。对于T2中的每个值,T1中都有一个值。
考虑到我在T2中有两种类型('A','B'),我必须计算映射到每个T1键的T2的数量,状态为'A'和'B'并将其设置为T1 .state分别
如何在Oracle中完成此任务?
与下面的帖子类似(但解决方案不起作用): how to update a column in a table with unique values, but not all the rows, not incremented
使用上面的源代码 - 我使用了以下脚本:
UPDATE T1
SET VALUE =
CASE
WHEN (SELECT STATE FROM T1 WHERE KEY = EACH_KEY) = 'S1'
THEN
(SELECT COUNT(*) FROM T2
WHERE KEY = EACH_KEY AND TYPE = 'A'
WHEN (SELECT STATE FROM T1 WHERE KEY = EACH_KEY) = 'S2'
THEN
(SELECT COUNT(*) FROM T2
WHERE KEY = EACH_KEY AND TYPE = 'B'
END
WHERE EACH_KEY in (select distinct KEY from T1);
但是oracle会抛出以下错误:
Error at Command Line : 13 Column : 8
Error report -
SQL Error: ORA-00904: "EACH_KEY": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
编辑#1:
在表格中采样数据
执行前
T1
key numval state
1 0 S1
2 0 S2
3 0 S1
4 0 S1
T2
key t1key type
1 1 A
2 1 B
3 1 B
4 2 A
5 2 B
6 2 B
7 3 B
8 4 A
9 4 A
10 4 B
执行后(预期结果)
T1
key numval state
1 1 S1
2 2 S2
3 0 S1
4 2 S2
编辑#2:
是的,未定义标识符。我想知道如何在Oracle SQL中定义这样的标识符,以便可以执行上述计算。
答案 0 :(得分:2)
以下是支持@ jva答案的测试用例:
使用数据创建表格:
create table t1 as
select 1 key, 0 numval, 'S1' state from dual union all
select 2 key, 0 numval, 'S2' state from dual union all
select 3 key, 0 numval, 'S1' state from dual union all
select 4 key, 0 numval, 'S1' state from dual;
create table t2 as
select 1 key, 1 t1key, 'A' type from dual union all
select 2 key, 1 t1key, 'B' type from dual union all
select 3 key, 1 t1key, 'B' type from dual union all
select 4 key, 2 t1key, 'A' type from dual union all
select 5 key, 2 t1key, 'B' type from dual union all
select 6 key, 2 t1key, 'B' type from dual union all
select 7 key, 3 t1key, 'B' type from dual union all
select 8 key, 4 t1key, 'A' type from dual union all
select 9 key, 4 t1key, 'A' type from dual union all
select 10 key, 4 t1key, 'B' type from dual;
检查t1中的初始值:
select * from t1 order by key;
KEY NUMVAL STATE
---------- ---------- -----
1 0 S1
2 0 S2
3 0 S1
4 0 S1
执行更新:
update t1
set numval = case when state = 'S1' then (select count(*) from t2 where t2.t1key = t1.key and t2.type = 'A')
when state = 'S2' then (select count(*) from t2 where t2.t1key = t1.key and t2.type = 'B')
end;
commit;
检查t1中的新值:
select * from t1 order by key;
KEY NUMVAL STATE
---------- ---------- -----
1 1 S1
2 2 S2
3 0 S1
4 2 S1
如您所见,这是您想要的输出。
来自"缺陷"你在@ jva的回答评论中提到过,我觉得你很困惑:
我强烈建议您查看the documentation,具体来说:
当嵌套子查询引用从子查询上一级父级语句引用的表中的列时,Oracle会执行相关子查询。父语句可以是SELECT,UPDATE或DELETE语句,子查询嵌套在该语句中。对于父语句处理的每一行,概念上对相关子查询进行一次计算。
答案 1 :(得分:1)
UPDATE T1
SET numval = CASE WHEN t1.state = 'S1' THEN (SELECT COUNT(*)
FROM t2
WHERE t1.key = t2.t1key
AND type = 'A')
WHEN t1.state = 'S2' THEN (SELECT COUNT(*)
FROM t2
WHERE t1.key = t2.t1key
AND type = 'B')
END