日期:2014-05-17  浏览次数:20827 次

求一条sql语句的写法,谢谢各位的帮忙
A表:
姓名 开始日期 结束日期
张三 2012-04-01 2012-04-03
李四 2012-04-01 2012-04-02
王五 2012-04-05 2012-04-06
求一sql(统计汇总),将A表转换成如下表
结果表:
总人数 日期
  2 2012-04-01
  2 2012-04-02
  1 2012-04-03
  1 2012-04-05
  1 2012-04-06

------解决方案--------------------
SQL code

with a as(
select '张三' xm,to_date('2012-04-01','yyyy-mm-dd') ksrq,to_date('2012-04-03','yyyy-mm-dd') jsrq from dual
union all
select '李四',to_date('2012-04-01','yyyy-mm-dd'),to_date('2012-04-02','yyyy-mm-dd') from dual
union all
select '王五',to_date('2012-04-05','yyyy-mm-dd'),to_date('2012-04-06','yyyy-mm-dd') from dual
)
select count(1), ksrq + rn - 1
  from a,
       (select rownum rn
          from dual
        connect by rownum <= (select max(jsrq - ksrq) from a)) t
 where a.jsrq >= a.ksrq + rn - 1
 group by ksrq + rn - 1
  COUNT(1) KSRQ+RN-1
---------- -----------
         2 2012-4-1
         2 2012-4-2
         1 2012-4-3
         1 2012-4-5
         1 2012-4-6

------解决方案--------------------
--怎么是个重复的帖子啊?
SQL code
select count(日期),日期 from 
(
select 开始日期 日期 from A
union all
select 结束日期 日期 from A
)
group by 日期

------解决方案--------------------
探讨

--怎么是个重复的帖子啊?
SQL code
select count(日期),日期 from

select 开始日期 日期 from A
union all
select 结束日期 日期 from A

group by 日期