+ -
当前位置:首页 → 问答吧 → 行转列,行合并,日期转星期问题

行转列,行合并,日期转星期问题

时间:2011-12-02

来源:互联网

表一
id date begin end
1 2011-12-2 09:30 10:00
1 2011-12-2 10:30 11:00
1 2011-12-3 09:30 10:00
2 2011-12-2 09:30 10:00
2 2011-12-2 10:30 11:00
2 2011-12-4 09:30 10:00
3 2011-12-2 09:30 10:00
3 2011-12-2 10:30 11:00
3 2011-12-4 09:30 10:00
表二
id 星期五 星期六 星期天  
-----------------------------------------------------  
1 09:30-10:00; 09:30-10:00; ( )
  10:30-11:00;  
-----------------------------------------------------
2 09:30-10:00; ( ) 09:30-10:00;
  10:30-11:00; 
-----------------------------------------------------
3 09:30-10:00; ( ) 09:30-10:00;
  10:30-11:00; 
-----------------------------------------------------

表一转表二 如何用sql语句实现.



作者: mojo_mask   发布时间: 2011-12-02

SQL code
with t  as (
 select 1 id ,date '2011-12-2' ddate, '09:30' begin, '10:00' end from  dual union all                                            
 select 1 id ,date '2011-12-2' ddate, '10:30' begin, '11:00' end from  dual union all
 select 1 id ,date '2011-12-3' ddate, '09:30' begin, '10:00' end from  dual union all
 select 2 id ,date '2011-12-2' ddate, '09:30' begin, '10:00' end from  dual union all
 select 2 id ,date '2011-12-2' ddate, '10:30' begin, '11:00' end from  dual union all
 select 2 id ,date '2011-12-4' ddate, '09:30' begin, '10:00' end from  dual union all
 select 3 id ,date '2011-12-2' ddate, '09:30' begin, '10:00' end from  dual union all
 select 3 id ,date '2011-12-2' ddate, '10:30' begin, '11:00' end from  dual union all
 select 3 id ,date '2011-12-4' ddate, '09:30' begin, '10:00' end from  dual 
 )
 select id ,wm_concat(a),wm_concat(b),wm_concat(c) from (
 select  id,decode(to_char(ddate,'dy'),'星期五',(begin||'-'||end)) a,
            decode(to_char(ddate,'dy'),'星期六',(begin||'-'||end)) b,
           decode(to_char(ddate,'dy'),'星期日',(begin||'-'||end)) c
           from t ) tt
           group by tt.id

----- 

1 09:30-10:00,10:30-11:00 09:30-10:00
2 09:30-10:00,10:30-11:00 09:30-10:00
3 09:30-10:00,10:30-11:00 09:30-10:00

作者: dws2004   发布时间: 2011-12-02

我晕,粘贴结果怎么这样;额
SQL code

    1    09:30-10:00,10:30-11:00    09:30-10:00    
    2    09:30-10:00,10:30-11:00        09:30-10:00
    3    09:30-10:00,10:30-11:00        09:30-10:00

作者: dws2004   发布时间: 2011-12-02

引用 1 楼 dws2004 的回复:

SQL code
with t as (
select 1 id ,date '2011-12-2' ddate, '09:30' begin, '10:00' end from dual union all
select 1 id ,date '2011-12-2' ddate, '10:30' ……

那不是有几条数据那我就要写几条数据

作者: mojo_mask   发布时间: 2011-12-02

SQL code

with t  as (
 select 1 id ,date '2011-12-2' ddate, '09:30' begin, '10:00' end from  dual union all                                            
 select 1 id ,date '2011-12-2' ddate, '10:30' begin, '11:00' end from  dual union all
 select 1 id ,date '2011-12-3' ddate, '09:30' begin, '10:00' end from  dual union all
 select 2 id ,date '2011-12-2' ddate, '09:30' begin, '10:00' end from  dual union all
 select 2 id ,date '2011-12-2' ddate, '10:30' begin, '11:00' end from  dual union all
 select 2 id ,date '2011-12-4' ddate, '09:30' begin, '10:00' end from  dual union all
 select 3 id ,date '2011-12-2' ddate, '09:30' begin, '10:00' end from  dual union all
 select 3 id ,date '2011-12-2' ddate, '10:30' begin, '11:00' end from  dual union all
 select 3 id ,date '2011-12-4' ddate, '09:30' begin, '10:00' end from  dual 
 )



这些语句的目的是为了建立测试数据,你有实际的表,不用写前面这些。
直接从实际表中的查询即可。

作者: LuiseRADL   发布时间: 2011-12-02

引用 4 楼 luiseradl 的回复:

SQL code

with t as (
select 1 id ,date '2011-12-2' ddate, '09:30' begin, '10:00' end from dual union all
select 1 id ,date '2011-12-2' ddate, '10:30'……

3Q,感谢

作者: mojo_mask   发布时间: 2011-12-02