我有这样一张表及数据:
日期  班次  值日人员
6月1日   白    张飞
6月1日   夜    刘备
6月3日   白    黄忠
6月3日   夜    赵云
6月8日   白    关羽
6月9日   夜    张飞
6月10日  白    曹操
6月20日  白    赵云我想生成这样一张视图:
从 6月1日 到 6月30日都有记录,即使这天没人值班。日期       白       夜
6月1日       张飞     刘备
  :
  :
  :        
6月20日      赵云     null
  :
  :
6月30日      null     null
求高手指点。谢谢!oracle  

解决方案 »

  1.   

    with tt as(
    select '6月1日' 日期,'白' 班次,'张飞' 值日人员 from dual union all
    select '6月1日','夜','刘备' from dual union all
    select '6月3日','白','黄忠' from dual union all
    select '6月3日','夜','赵云' from dual union all
    select '6月8日','白','关羽' from dual union all
    select '6月9日','夜','张飞' from dual union all
    select '6月10日','白','曹操' from dual union all
    select '6月20日','白','赵云' from dual)
    select 日期,wm_concat(decode(班次,'白',值日人员)) 白,wm_concat(decode(班次,'夜',值日人员)) 夜 
    from tt 
    group by 日期;
      

  2.   

    with tt as
     (select '2013-06-01' 日期, '白' 班次, '张飞' 值日人员
        from dual
      union all
      select '2013-06-01', '夜', '刘备'
        from dual
      union all
      select '2013-06-03', '白', '黄忠'
        from dual
      union all
      select '2013-06-03', '夜', '赵云'
        from dual
      union all
      select '2013-06-08', '白', '关羽'
        from dual
      union all
      select '2013-06-09', '夜', '张飞'
        from dual
      union all
      select '2013-06-10', '白', '曹操'
        from dual
      union all
      select '2013-06-20', '白', '赵云' from dual)
    select rq,
           max(decode(班次, '白', 值日人员, null)),
           max(decode(班次, '夜', 值日人员, null))
      from tt,
           (select '2013-06-' || lpad(rownum, 2, '0') rq
              from dual
            connect by rownum <= to_number(to_char(last_day(trunc(to_date('2013-06-01',
                                                                          'yyyy-mm-dd'))),
                                                   'DD'))) t1
     where t1.rq = tt.日期(+)
     group by rq
     order by rq
      

  3.   

    做法都差不多 参数 传入一个年月就可以了with t as
    (
      select date'2013-06-01' tdate, '白' bc, '张飞' pl from dual union all
      select date'2013-06-01', '夜', '刘备' from dual union all
      select date'2013-06-03', '白', '黄忠' from dual union all
      select date'2013-06-03', '夜', '赵云' from dual union all
      select date'2013-06-08', '白', '关羽' from dual union all
      select date'2013-06-09', '夜', '张飞' from dual union all
      select date'2013-06-10', '白', '曹操' from dual union all
      select date'2013-06-20', '白', '赵云' from dual
      )
    select adate "日期",
        max(decode(bc, '白', pl, null)) "白",
        max(decode(bc, '夜', pl, null)) "夜"
    from
    (
        select to_date('2013-06','yyyy-mm')+level-1 adate
        from dual
        connect by level <= add_months(to_date('2013-06','yyyy-mm'),1)-to_date('2013-06','yyyy-mm')
    ) t1 left join t on t1.adate = t.tdate
    group by t1.adate
    order by t1.adate    日期    白    夜   
    ----------------------------------------
    1 2013/6/1 张飞 刘备
    2 2013/6/2
    3 2013/6/3 黄忠 赵云
    4 2013/6/4
    5 2013/6/5
    ......