我们如何将多行放入单行

时间:2015-11-24 12:46:08

标签: oracle oracle11g

with ces as
 (select account_num,
 imsi,
 previous_month,
 current_month,
 status
  from (
select  account_num,
        imsi,
           to_char(u.previous_month,'yyyymmdd')previous_month,
             to_char(u.current_month,'yyyymmdd')current_month,
             ls.status  
             from ( select  account_num
               ,imsi
               ,case when usage_date < add_months(to_date('20151110','yyyymmdd'),-1) then usage_date
               else null
              end previous_month
               ,case when usage_date >= add_months(to_date('20151110','yyyymmdd'),-1) then usage_date
               else null
              end current_month
                            from genevabatchuser.loadusagedata
                               where event_source = 'NUMEREXROMHYB' 
                                and account_num = 'ACC000142'
             and imsi in ('310640100000003','310640100000004','310640100000338','310640100000331')
                                 )u full outer join GENEVABATCHUSER.TMOLOADIMSILIST ls  using (account_num,imsi)
               )
               )
               select * from ces 
               where imsi in ('310640100000003','310640100000004','310640100000338','310640100000331')

我看到如下输出:

ACCOUNT_NUM    IMSI           PREVIOUS_MONTH         CURRENT_MONTH
ACC000142   310640100000003  10/8/2015 5:18:29 AM   
ACC000142   310640100000003                        11/3/2015 11:25:33 PM
ACC000142   310640100000003                         11/3/2015 5:18:29 AM
ACC000142   310640100000003  10/8/2015 11:25:33 PM  

我的预期输出是:

ACCOUNT_NUM    IMSI           PREVIOUS_MONTH             CURRENT_MONTH
ACC000142   310640100000003  10/8/2015 5:18:29 AM   11/3/2015 11:25:33 PM

1 个答案:

答案 0 :(得分:0)

看来你想要做的就是

with ces as (select account_num,
                    imsi,
                    u.previous_month,
                    u.current_month
                    ls.status  
               from (select account_num,
                            imsi,
                            case
                              when usage_date < add_months(to_date('20151110','yyyymmdd'),-1) then usage_date
                              else null
                            end previous_month,
                            case
                              when usage_date >= add_months(to_date('20151110','yyyymmdd'),-1) then usage_date
                              else null
                            end current_month
                       from genevabatchuser.loadusagedata
                       where event_source = 'NUMEREXROMHYB' and
                             account_num = 'ACC000142' and
                             imsi in ('310640100000003','310640100000004','310640100000338','310640100000331'))u
               full outer join GENEVABATCHUSER.TMOLOADIMSILIST ls  
                 using (account_num, imsi))
select ACCOUNT_NUM,
       IMSI,
       MIN(PREVIOUS_MONTH) AS PREVIOUS_MONTH,
       MAX(CURRENT_MONTH) AS CURRENT_MONTH
  from ces 
  where imsi in ('310640100000003','310640100000004','310640100000338','310640100000331')
  GROUP BY ACCOUNT_NUM, IMSI;

祝你好运。