How to convert SQL Unpivot query to HANA SQL?

匆匆过客 提交于 2021-02-15 07:43:43

问题


How to convert following SQL Server query which uses unpivot to a HANA SQL query.

SELECT 
    p.CardName, 
    REPLACE(p.groupcode1, 'QryGroup', '') groupcode1, 
    ocqg.GroupName
FROM
    ocrd 
UNPIVOT 
    (value
     FOR groupcode1 IN ([QryGroup1], [QryGroup2], [QryGroup3], [QryGroup4], 
                        [QryGroup5], [QryGroup6], [QryGroup7], [QryGroup8],
                        [QryGroup9], [QryGroup10], [QryGroup11], [QryGroup12],
                        [QryGroup13], [QryGroup14], [QryGroup15],[QryGroup16],
                        [QryGroup17], [QryGroup18], [QryGroup19],[QryGroup20],
                        [QryGroup21], [QryGroup22], [QryGroup23],[QryGroup24],
                        [QryGroup25], [QryGroup26], [QryGroup27],[QryGroup28],
                        [QryGroup29], [QryGroup30], [QryGroup31],[QryGroup32],
                        [QryGroup33], [QryGroup34], [QryGroup35],[QryGroup36],
                        [QryGroup37], [QryGroup38], [QryGroup39],[QryGroup40],
                        [QryGroup41], [QryGroup42], [QryGroup43],[QryGroup44],
                        [QryGroup45], [QryGroup46], [QryGroup47],[QryGroup48],
                        [QryGroup49], [QryGroup50], [QryGroup51],[QryGroup52],
                        [QryGroup53], [QryGroup54], [QryGroup55],[QryGroup56],
                        [QryGroup57], [QryGroup58], [QryGroup59],[QryGroup60],
                        [QryGroup61], [QryGroup62], [QryGroup63],[QryGroup64])

     ) as p,
     ocqg
WHERE 
    value = 'Y' 
    AND ocqg.GroupCode = REPLACE(p.groupcode1, 'QryGroup', '')
ORDER BY 
    p.CardCode

回答1:


I don't think Hana SQL supports lateral joins (the apply keyword in SQL Server). One method is just union all:

select og.*
from (select o.cardname, g.groupcode,
             (case when g.groupcode = 1 then groupcode1
                   when g.groupcode = 2 then groupcode2
                   . . .
                   when g.groupcode = 64 then groupcode64
              end) as groupname
      from ocrd o cross join
           (select 1 as groupcode from dummy union all
            select 2 as groupcode from dummy union all
            . . .
            select 64 as groupcode from dummy
           ) g   
     ) og         
where og.groupname is not null;


来源:https://stackoverflow.com/questions/60912704/how-to-convert-sql-unpivot-query-to-hana-sql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!