I need to add a column to a table then fill it with computed values from a select (from the orignal table itself) like this
create table yo (ad integer primary key, pa integer, pd integer);
insert into yo values
(1,1,1),(2,1,3),(3,1,4),(4,3,5),(5,4,2),(6,3,8),(7,1,9),(8,6,7),(9,3,6);
.header on
.mode column yo;
select * from yo;
ad pa pd
---------- ---------- ----------
1 1 1
2 1 3
3 1 4
4 3 5
5 4 2
6 3 8
7 1 9
8 6 7
9 3 6
The 'ad' col is a kinda address, the 'pa' col is a parent address, the 'pd' col is a kinda ID or value for that row. I'd like to add a col named 'pp' that would be a 'parent ID' that would be computed like this
select yo2.pd from yo yo1 join yo yo2 on yo2.ad=yo1.pa;
pd
----------
1
1
1
4
5
4
1
8
4
So I'd like to setup 'pp' whit this output I tried this but obviously I must miss somethings
alter table yo add column pp integer;
update yo set pp =
(select yo2.pd as pp from yo yo1 join yo yo2 on yo2.ad=yo1.pa);
ad pa pd pp
---- --- --- ----
1 1 1 1
2 1 3 1
3 1 4 1
4 3 5 1
5 4 2 1
6 3 8 1
7 1 9 1
8 6 7 1
9 3 6 1
Does what I want to achieve is doable, I so othre link on similar proble that I don't understand the answers, and I found also tiny case like this
create table t1 (a int, b int);
insert into t1 values (1,0),(2,0);
create table t2 (a int, c int);
insert into t2 values (1,101),(2,102);
update t1 set b = (select c from t2 where t2.a = t1.a) where t1.b = 0;
This one do update correctly so why my select differ from this later one?
Thanx for any advise
Phi
Aucun commentaire:
Enregistrer un commentaire