表A
  员工姓名    年月      换休小时    累计换休    年假小时
  aaa       200701      123       222         333
  aaa       200702      123       222         333
  aaa       200703      123       222         333
  aaa       200705      333       333         333
  aaa       200712      333       333         333
  bbb       200701      123       55          122
  bbb       200702      21        233         123

表B
  员工姓名 年月  1月换休小时   1月累计换休  1月年假   2月换休小时  2月累计换休  2月年假 ...
  aaa      2007    123           222         333        123          222       333 ...
  bbb      2007    563           565         656       5656          565      5656 ...
  ccc      2007      0             0           0          0            0         0 ...目的: 从表A取数据插入到表B中遇到的问题:
1. 表A中 有可能 aaa    在5 6 月都没有记录 ,如何让当没记录的时候,值为 0
2.如何根据表A中 月份的不同 ,对应着插入到表B,而且当没有记录的时候,值为0

解决方案 »

  1.   

    只写了三个月的,其他的月自己加吧 ...
    我测试是成功的(先运行select里面的语句就可以看到结果),你试试看~~~
    insert into A
    select tt.name,
           to_char(tt.yearmonth,'yyyy') as datetime,
           max(nvl(decode(to_char(tt.yearmonth,'mm'),'01',tt.changehour),0)) as "1 changehour",
           max(nvl(decode(to_char(tt.yearmonth,'mm'),'01',tt.addchange),0)) as "1 addchange",
           max(nvl(decode(to_char(tt.yearmonth,'mm'),'01',tt.yearhour),0)) as "1 yearhour",
           max(nvl(decode(to_char(tt.yearmonth,'mm'),'02',tt.changehour),0)) as "2 changehour",
           max(nvl(decode(to_char(tt.yearmonth,'mm'),'02',tt.addchange),0)) as "2 addchange",
           max(nvl(decode(to_char(tt.yearmonth,'mm'),'02',tt.yearhour),0)) as "2 yearhour",
           max(nvl(decode(to_char(tt.yearmonth,'mm'),'03',tt.changehour),0)) as "3 changehour",
           max(nvl(decode(to_char(tt.yearmonth,'mm'),'03',tt.addchange),0)) as "3 addchange",
           max(nvl(decode(to_char(tt.yearmonth,'mm'),'03',tt.yearhour),0)) as "3 yearhour"
      from B tt
     group by tt.name,to_char(tt.yearmonth,'yyyy');=======================result==================================================NAME DATETIME 1 changehour 1 addchange 1 yearhour 2 changehour 2 addchange 2 yearhour 3 changehour 3 addchange 3 yearhour
    ---- -------- ------------ ----------- ---------- ------------ ----------- ---------- ------------ ----------- ----------
    aaa  2007              123         222        333          123         222        333          123         222        333
    bbb  2007              123          55        122           21         233        123            0           0          0
      

  2.   

    mantisXF(枫の叶)  太感谢你了这个方法比我之前想的简单多了。
    激动得眼泪哗哗的