I am trying to do an insert into table2 based on a select from table1, but I can not get the correct syntax. The column names from table1 will drive the value being inserted into the PD_NO column in table2, as shown in the example below. Can anyone help with this?
Table1:
(1) (2) (3) (4) (5) (6)
| SEQ | PD_01 | PD_02 | PD_03 | PD_04 | PD_05 | PD_06 |
|-----+-------+-------+-------+-------+-------+-------|
| 632 | 10000 | 0 | 500 | 0 | 20000 | 0 |
Table2:
| SEQ | PD_NO | AMT |
|-----+-------+-------|
| 632 | 1 | 10000 |
|-----+-------+-------|
| 632 | 3 | 500 |
|-----+-------+-------|
| 632 | 5 | 20000 |
|-----+-------+-------|
I know if I am working the other direction (inserting contents of table2 into table1) that I can do something like the following:
INSERT INTO table1
SELECT
seq,
SUM (CASE WHEN pd_no = 1 THEN amt ELSE 0 END) p01_amt,
SUM (CASE WHEN pd_no = 2 THEN amt ELSE 0 END) p02_amt,
SUM (CASE WHEN pd_no = 3 THEN amt ELSE 0 END) p03_amt,
SUM (CASE WHEN pd_no = 4 THEN amt ELSE 0 END) p04_amt,
SUM (CASE WHEN pd_no = 5 THEN amt ELSE 0 END) p05_amt,
SUM (CASE WHEN pd_no = 6 THEN amt ELSE 0 END) p06_amt
FROM table2;