开发者

Update table1 based on value from table 2 in teradata

I have two tab开发者_开发百科les like this

Update table1 based on value from table 2 in teradata

I would like to insert from Table1 to Table2 here. This is how I want it.

Take MOU = 10. It has num1 and hour1 in the same row. I would like to insert it into the cell that is at the same row as that of num1 and same column as that of hour1.

How could I do that?

Disclaimer: I am not offering any code here because I am unsure of how to write this query. I sure do know to write a simple update. I am a teracota newbie.


This worked.

UPDATE a
FROM table2 a, table1 b
SET hour1=b.mou
WHERE a.access_method_id=b.access_method_id
AND hour='hour1'

Did the same for each hours. Not very elegant. But this is all I could get.


Here is some generic SQL that should get the job done.

insert into table2(access_method_id, hour1, hour2, ...)
select 
  access_method_id, 
  sum(case when hour='HOUR1' then MOU else 0 end) as hour1,
  sum(case when hour='HOUR2' then MOU else 0 end) as hour2,
  ...etc
from
  table1
group by
  access_method_id


try this!

update table2 t2
from (select 
  access_method_id, 
  sum(case when hour='HOUR1' then MOU else 0 end) as hour1,
  sum(case when hour='HOUR2' then MOU else 0 end) as hour2,
  ...etc
from
  table1) t1
set
t2.hour1=t1.hour1,
t2.hour2=t1.hour2,
t2.hour3=t1.hour3,
...etc
where t2.access_method_id=t1.access_method_id;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜