首页 > 数据库技术 > 详细

oracle中列转行写法

时间:2020-11-18 15:24:39      阅读:40      评论:0      收藏:0      [点我收藏+]
--查询为列的原始代码
SELECT
1 AS num, t.id, t.company_shorthand AS CountryCode FROM qhyscm.tab_base_company t WHERE t.company_type = ‘1‘ AND t.company_shorthand = ‘EORLK‘ UNION ALL SELECT 2 AS num, t.id, t.company_shorthand AS CountryCode FROM qhyscm.tab_base_company t WHERE t.company_type = ‘2‘ AND t.company_shorthand = ‘IORLK‘ UNION ALL SELECT 3 AS num, t.id, t.company_shorthand AS CountryCode FROM qhyscm.tab_base_company t WHERE t.company_type = ‘3‘ AND t.company_shorthand = ‘SORLK‘ UNION ALL SELECT 4 AS num, t.id, t.company_shorthand AS CountryCode FROM qhyscm.tab_base_company t WHERE t.company_type = ‘4‘ AND t.company_shorthand = ‘LK‘

技术分享图片

 

 列转行写法

SELECT
    *
FROM
    (
    SELECT
        1 AS num, t.id
    FROM
        qhyscm.tab_base_company t
    WHERE
        t.company_type = ‘1‘
        AND t.company_shorthand = ‘EORLK‘
UNION ALL
    SELECT
        2 AS num, t.id
    FROM
        qhyscm.tab_base_company t
    WHERE
        t.company_type = ‘2‘
        AND t.company_shorthand = ‘IORLK‘
UNION ALL
    SELECT
        3 AS num, t.id
    FROM
        qhyscm.tab_base_company t
    WHERE
        t.company_type = ‘3‘
        AND t.company_shorthand = ‘SORLK‘
UNION ALL
    SELECT
        4 AS num, t.id
    FROM
        qhyscm.tab_base_company t
    WHERE
        t.company_type = ‘4‘
        AND t.company_shorthand = ‘LK‘) pivot( max(id) FOR num IN ( 1 AS eor, 2 AS ior, 3 AS sor, 4 AS wl ) )
ORDER BY
    1

技术分享图片

 

oracle中列转行写法

原文:https://www.cnblogs.com/wangquanyi/p/13999964.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!