首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > oracle >

求统计某个月全部天的统计信息

2013-07-16 
求统计某个月所有天的统计信息我有这样一张表及数据:日期班次值日人员6月1日白张飞6月1日夜刘备6月3日白黄

求统计某个月所有天的统计信息
我有这样一张表及数据:
日期  班次  值日人员
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??
[解决办法]
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 日期;
[解决办法]

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



[解决办法]
做法都差不多 参数 传入一个年月就可以了

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

    日期    白    夜   
----------------------------------------
12013/6/1张飞刘备
22013/6/2
32013/6/3黄忠赵云
42013/6/4
52013/6/5
......

热点排行