Update table1 based on value from table 2 in teradata

24,272

Solution 1

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.

Solution 2

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
Share:
24,272
naveen
Author by

naveen

ASP.NET Consultant, JavaScript Enthusiast, Lover, Husband, Father. In other words, yet another coder... Email: Click here

Updated on May 07, 2020

Comments

  • naveen
    naveen about 4 years

    I have two tables like this

    enter image description here

    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.