2017-09-08 52 views
-3

尋找從table1到table2填充數據的幫助。 我有以下列的table1和數據示例如下。 當天是5,它需要填充上其它表2基於天10小時的數據(飽和和太陽需要是0)PLsql或Oracle Sql登錄帳戶

表1:

id year month  days 
101 2017 September 5 
102 2017 September 4 
103 2017 September 3 
104 2017 September 2 

表2需要被填充作爲如下:

Id Month  1 2(Sat) 3(Sunday) 4 5 
101 September 8 0  0 8 8 8 8 
102 September 8 0  0 8 8 8 0 
103 September 8 0  0 8 8 0 0 
104 September 8 0  0 8 0 0 0 
+1

它不是很清楚 –

回答

0

你得到想要的結果:

select 
    id, 
    month, 
    case when days >= 1 then 8 end as friday, 
    0        as saturday, 
    0        as sunday, 
    case when days >= 2 then 8 end as monday, 
    case when days >= 3 then 8 end as tuesday, 
    case when days >= 4 then 8 end as wednesday, 
    case when days >= 5 then 8 end as thursday 
from table1 
where year = 2017; 

爲了用這個數據填充table2,你可以使用插入選擇:

insert into table2 (id, month, saturday, sunday, monday, tuesday, wednesday, thursday) 
select 
    id, 
    ... 
from table1 
where year = 2017;