元数据如下:
司机           车次       车号               车站                             时间                       上下车
001           1305      1305              1399               2017/5/18 5:13:01                 0
001           1305      1305              1321               2017/5/18 5:35:53                 1
001           1305      130581          1321               2017/5/18 5:36:01                 0
001           1305      130581          1321               2017/5/18 7:51:01                 1
001           1305      130581          1321               2017/5/18 7:52:01                 0
001           1305      130581          1399               2017/5/18 8:13:01                 1效果司机          车次          车号               车站                 时间                         上车          车站                   时间                       下车  
001          1305         1305             1399         2017/5/18 5:13:01           0            1321            2017/5/18 5:35:53        1
001          1305         130581         1321         2017/5/18 5:36:01           0            1321            2017/5/18 5:51:01        1
001          1305         130581         1321         2017/5/18 7:52:01           0            1399            2017/5/18 8:13:01        1

解决方案 »

  1.   

    select 司机,车次,车号,wm_concat(上车) 上车,wm_concat(上车时间) 上车时间,,wm_concat(上车车站) 上车车站,wm_concat(下车) 下车,wm_concat(下车时间)  下车时间,,wm_concat(下车车站) 下车车站 from (
    select 司机,车站,
    decode(上下车,0,上下车,null)  上车,
    decode(上下车,0,时间,null)  上车时间,
    decode(上下车,0,车站,null)  上车车站,
    decode(上下车,1,上下车,null)  下车,
    decode(上下车,1,时间,null)  下车时间,
    decode(上下车,1,车站,null)  下车车站,
    from 

    )
    group by 司机,车次,车号
    order by 司机,车次,车号
      

  2.   


    -- 借楼主语句,用个 max 函数就行了
    select 司机,
           车站,
           车号,
           max(decode(上下车, 0, 上下车, null)) 上车,
           max(decode(上下车, 0, 时间, null)) 上车时间,
           max(decode(上下车, 0, 车站, null)) 上车车站,
           max(decode(上下车, 1, 上下车, null)) 下车,
           max(decode(上下车, 1, 时间, null)) 下车时间,
           max(decode(上下车, 1, 车站, null)) 下车车站
      from 表
     group by 司机, 车次, 车号
     order by 司机, 车次, 车号
      

  3.   


    你也可以这样写
    select a*,B.车站, B.时间, B.下车 FROM
    (select 司机,车次,车号,车站,  时间 ,上下车 AS 上车   from   T1  WHERE  上下车=0)A1
    join   (SELECT   司机,车次,车号,车站,  时间 ,上下车 AS 下车   from   T1  WHERE  上下车=1) A2
    ON A1.司机=A2.司机 AND A1.,车次=A2.,车次 AND A1.车号=A2.车号
      

  4.   

    select 司机,
           车站,
           车号,
           max(decode(上下车, 0, 上下车, null)) 上车,
           max(decode(上下车, 0, 时间, null)) 上车时间,
           max(decode(上下车, 0, 车站, null)) 上车车站,
           max(decode(上下车, 1, 上下车, null)) 下车,
           max(decode(上下车, 1, 时间, null)) 下车时间,
           max(decode(上下车, 1, 车站, null)) 下车车站
      from 表
     group by 司机, 车次, 车号
     order by 司机, 车次, 车号
    红色,改成 车次;