postgresql 行转列,列转行后加入到一个整体数据

这里行转列的基本思想就是使用max,因为其他列下面都是NULL,所以可以Max最后就只能得到有值的这行

普通的查询:

SELECT
 icd ,
 case when 
(ROW_NUMBER() OVER(PARTITION BY INNER_CD ORDER BY SLIDE_SEQ )) =1  then  SLIDE_QTY  END  as SLIDE_QTY1,
 case when                                                                                                                     
(ROW_NUMBER() OVER(PARTITION BY INNER_CD ORDER BY SLIDE_SEQ )) =1 then SLIDE_S_UNIT_PRICE END   as SLIDE_S_UNIT_PRICE1,
 case when                                                                                                                     
(ROW_NUMBER() OVER(PARTITION BY INNER_CD ORDER BY SLIDE_SEQ )) =2 then SLIDE_QTY END   as SLIDE_QTY2,
 case when                                                                                                                     
(ROW_NUMBER() OVER(PARTITION BY INNER_CD ORDER BY SLIDE_SEQ )) =3 then SLIDE_QTY END  as SLIDE_QTY3

FROM
    "temp"."user" mm
WHERE 
mm.icd = 'ADDM01-20160612-1-100002-001'

得到的数据是:

使用max后:

SELECT  
    MAX( case when  SLIDE_SEQ = 1 THEN SLIDE_QTY  END  )as SLIDE_QTY1,
    MAX( case when  SLIDE_SEQ = 2 THEN SLIDE_QTY  END  )as SLIDE_QTY2,
    MAX( case when  SLIDE_SEQ = 3 THEN SLIDE_QTY  END  )as SLIDE_QTY3,
    MAX( case when  SLIDE_SEQ = 4 THEN SLIDE_QTY  END  )as SLIDE_QTY4,
    MAX( case when  SLIDE_SEQ = 5 THEN SLIDE_QTY  END  )as SLIDE_QTY5,
    MAX( case when  SLIDE_SEQ = 6 THEN SLIDE_QTY  END  )as SLIDE_QTY6,
    MAX( case when  SLIDE_SEQ = 7 THEN SLIDE_QTY  END  )as SLIDE_QTY7,
    MAX( case when  SLIDE_SEQ = 8 THEN SLIDE_QTY  END  )as SLIDE_QTY8,
    MAX( case when  SLIDE_SEQ = 9 THEN SLIDE_QTY  END  )as SLIDE_QTY9,
    MAX( case when  SLIDE_SEQ = 10 THEN SLIDE_QTY  END  )as SLIDE_QTY10
FROM
user
WHERE 
anken_id = 'ADDM01-20160612-1-100002-001'

得到的结果:

然后可以通过子查询进行组合

    SELECT * from  
m_product INNER JOIN 
    ( SELECT  
    anken_id,
        icd,
        MAX( case when  SLIDE_SEQ = 1 THEN SLIDE_QTY  END  )as SLIDE_QTY1,
        MAX( case when  SLIDE_SEQ = 2 THEN SLIDE_QTY  END  )as SLIDE_QTY2,
        MAX( case when  SLIDE_SEQ = 3 THEN SLIDE_QTY  END  )as SLIDE_QTY3,
        MAX( case when  SLIDE_SEQ = 4 THEN SLIDE_QTY  END  )as SLIDE_QTY4,
        MAX( case when  SLIDE_SEQ = 5 THEN SLIDE_QTY  END  )as SLIDE_QTY5,
        MAX( case when  SLIDE_SEQ = 6 THEN SLIDE_QTY  END  )as SLIDE_QTY6,
        MAX( case when  SLIDE_SEQ = 7 THEN SLIDE_QTY  END  )as SLIDE_QTY7,
        MAX( case when  SLIDE_SEQ = 8 THEN SLIDE_QTY  END  )as SLIDE_QTY8,
        MAX( case when  SLIDE_SEQ = 9 THEN SLIDE_QTY  END  )as SLIDE_QTY9,
        MAX( case when  SLIDE_SEQ = 10 THEN SLIDE_QTY  END  )as SLIDE_QTY10
    FROM
    user
WHERE 
user.anken_id = 'ADDM01-20160612-1-100002-001'
GROUP BY user.inner_cd,user.anken_id) t1 on  m_product.anken_id = t1.anken_id
where t1.anken_id = 'ADDM01-20160612-1-100002-001'

就可以得到富含行转列数据的。

列转行的基本思路就是通过制定一个共同的字段,然后将查询来的数据一行一行的累加起来。

    SELECT
                aa,
                bb,
        c_1 AS cCount
    FROM
        TEMP .AAAA
    WHERE
        TEMP .AAAA.Id = 'ADDM01201606120001'
    UNION ALL
        SELECT
                aa,
                bb,
            c_2 AS cCount
        FROM
            TEMP .AAAA
        WHERE
            TEMP .AAAA.Id = 'ADDM01201606120001'
        UNION ALL
            SELECT
                aa,
                bb,
                c_3 AS cCount
            FROM
                TEMP .AAAA
            WHERE
                TEMP .AAAA.Id = 'ADDM01201606120001'

 

posted on 2016-06-20 18:03  手撕高达的村长  阅读(11111)  评论(0编辑  收藏  举报

导航