SQL 行转列

-- 第一种方法:
select t.organ_new_no,
       max(decode(t.cmp_brand_no, 'BA01', t.create_time, sysdate)) as BA01,
       max(decode(t.cmp_brand_no, 'BA02', t.create_time, sysdate)) as BA02,
       max(decode(t.cmp_brand_no, 'BA03', t.create_time, sysdate)) as BA03,
       max(decode(t.cmp_brand_no, 'BA04', t.create_time, sysdate)) as BA04
  from (
        select *
          from BRAND_TB t
         where t.organ_new_no = 'DB1'
           and t.cmp_brand_no in ('BA01', 'BA02', 'BA03', 'BA04')) t
 group by organ_new_no


-- 第二种方法:
select t.organ_new_no,
       max(case t.cmp_brand_no when 'BA01' then t.create_time else sysdate end) as BA01,
       max(case t.cmp_brand_no when 'BA02' then t.create_time else sysdate end) as BA01,
       max(case t.cmp_brand_no when 'BA03' then t.create_time else sysdate end) as BA03,
       max(case t.cmp_brand_no when 'BA04' then t.create_time else sysdate end) as BA04
  from (
        select *
          from BRAND_TB t
         where t.organ_new_no = 'DB1'
           and t.cmp_brand_no in ('BA01', 'BA02', 'BA03', 'BA04')) t
 group by organ_new_no

 

posted @ 2018-07-26 17:10  janederek  阅读(221)  评论(0)    收藏  举报