现在我有一个表,让我们说表PROPERTY,它有2列,分别叫做A和B,
A B PIVOT
1 dog T
2 cat T
1 chien F
1 gou F
2 chat F
2 miao F
现在我要添加一个C列,其内容基于A和B,并以1.表示,即
A B C
1 dog dog
2 cat cat
1 chien dog
1 gou dog
2 chat cat
2 miao cat
我该怎么做?这是一张大桌子(200k行)
我在考虑:
UPDATE PROPERTY a SET C=
(select min(b.B) from CFG_DIM_PROPERTY b where
b.A= a.A and b.PIVOT= 'T')
但是,这个sql花了太多时间。你聪明的人知道其他方式吗?还有一些关于你如何看待它的迹象?
答案 0 :(得分:0)
看起来你想要这样的输出?因为您的表还没有列c
。如果你想添加它,你可以使用下面的逻辑来获得它的价值。
另请注意,对于每个A
,如果PIVOT1
为T
,则可能会出现意外结果。
with tbl(A,B,PIVOT1) as(
select 1,'dog ','T' from dual union all
select 2,'cat ','T' from dual union all
select 1,'chien','F' from dual union all
select 1,'gou ','F' from dual union all
select 2,'chat ','F' from dual union all
select 2,'miao ','F' from dual
)
select t1.a,t1.b,t2.b as C from tbl t1 left join
(select a,b from tbl where pivot1='T') t2
on t1.a=t2.a
输出:
A B C
1 gou dog
1 chien dog
1 dog dog
2 miao cat
2 chat cat
2 cat cat
答案 1 :(得分:0)
UPDATE PROPERTY p
SET c = ( SELECT b
FROM PROPERTY v
WHERE p.a = v.a
AND v."PIVOT" = 'T'
AND ROWNUM = 1 -- This line may not be necessary if there
-- is only ever one 'T' per 'A'
);
答案 2 :(得分:0)
为什么不使用合并:
merge into PROPERTY t
using (select min (b) as min_b, a
from CFG_DIM_PROPERTY b
where b.pivot = 'T'
group by a) t1
on (t.a = t1.a)
when matched then
update set t.c = t1.min_b;