表结构和数据:
id    name   type   xmbh   accepttime 
1001  测试1   01    xmbh01  
1002  测试1   02    xmbh01  2009-02-02
1003  测试1   03    xmbh01  2009-02-03
1004  测试1   04    xmbh01  2009-02-04 1005  测试2   01    xmbh02  2009-03-01
1006  测试3   02    xmbh03  2009-04-02
1007  测试4   03    xmbh04  2009-05-03
1008  测试5   04    xmbh05  2009-06-041009  测试6   03    xmbh06  2009-07-04
1010  测试6   04    xmbh06  2009-08-04数据描叙:
现假设有一个项目,name为项目名称,xmbh项目编号 
accepttime为数据插入数据库时的时间,type为阶段标识我们想象完成一个项目需要9个阶段,阶段一...阶段九,对应表中字段type(01...09),
每完成一个阶段会在上表中插入一条数据,如上表xmbh=‘xmbh01’的已经流到第四阶段了
假设xmbh=‘xmbh01’ 在01阶段的accepttime为空 ,在05至09阶段还没有生成数据
现需要完成一个查询,查询列表如下(界面显示效果):显示结果:
   项目名    项目编号   第一阶段          第二阶段       第三阶段          第四阶段      第五阶段...第九阶段
   测试1     xmbh01        无      2009-02-02     2009-02-03       2009-02-04     
   测试2     xmbh02  2009-03-01
   测试3     xmbh03                 2009-04-02
   测试4     xmbh04                                2009-05-03
   测试5     xmbh05                                                 2009-06-04
   测试6     xmbh06                                2009-07-04       2009-08-04   显示说明:
同一个项目在各个阶段的生成时间,将项目的名称,项目编号,以及各个阶段的生成时间组成一个12列,
多行的列表.行由项目决定,有多少个select distinct(xmbh)....where xmbh is not null就有多少行

解决方案 »

  1.   

    select t.name,t.xmbh,nvl(max(decode(type,'01',to_char(t.accepttime,'yyyy-mm-dd'))),'无') 第1阶段,
    nvl(max(decode(type,'02',to_char(t.accepttime,'yyyy-mm-dd'))),'无') 第2阶段,
    nvl(max(decode(type,'03',to_char(t.accepttime,'yyyy-mm-dd'))),'无') 第3阶段,
    nvl(max(decode(type,'04',to_char(t.accepttime,'yyyy-mm-dd'))),'无') 第4阶段,
    nvl(max(decode(type,'05',to_char(t.accepttime,'yyyy-mm-dd'))),'无') 第5阶段,
    .......
     from test5 t group by t.name,t.xmbh;
      

  2.   

    select  a.name,
    a.xmbh,
    (select t1.accepttime from table t1 where t1.xmbh=a.xmbh and t1.type=01 ) as type1,
    (select t2.accepttime from table t2 where t2.xmbh=a.xmbh and t2.type=02 ) as type2,
    (select t3.accepttime from table t3 where t3.xmbh=a.xmbh and t3.type=03 ) as type3,
    (select t4.accepttime from table t4 where t4.xmbh=a.xmbh and t4.type=04 ) as type4,
    (select t5.accepttime from table t5 where t5.xmbh=a.xmbh and t5.type=05 ) as type5,
    (select t6.accepttime from table t6 where t6.xmbh=a.xmbh and t6.type=06 ) as type6,
    (select t7.accepttime from table t7 where t7.xmbh=a.xmbh and t7.type=07 ) as type7,
    (select t8.accepttime from table t7 where t8.xmbh=a.xmbh and t8.type=08 ) as type8,
    (select t9.accepttime from table t9 where t9.xmbh=a.xmbh and t9.type=09 ) as type9,
    from (select distinct name,xmbh from table) a
    order by a.xmbh
      

  3.   

    行列转换,还是固定列数的,这就简单了
    去GOOGLE或百度一下行列转换一堆数据
      

  4.   

    同意4楼,如果列回定就用DECODE,如果不固定就写个存储过程。
      

  5.   

    使用分析语句可以实现 connect by