zoukankan      html  css  js  c++  java
  • Oracle 转 postgresql 递归 connect_by_isleaf 方案

    oracle:

    SELECT
            user_number,
            LTRIM( SYS_CONNECT_BY_PATH ( NAME, ',' ), ',' ) NAME 
            ,RN
        FROM
            (
            SELECT
                u.user_number,
                r.NAME,
                ROW_NUMBER ( ) OVER ( PARTITION BY u.user_number ORDER BY ur.role_id ) RN 
            FROM
                ems_role r,
                ( SELECT ur.user_id, ur.role_id FROM ems_user_role ur 
                UNION SELECT ur.user_id, ur.role_id FROM ems_user_device_role ur ) ur,
                ems_user u 
            WHERE
                ur.user_id = u.ID 
                AND r.ID = ur.role_id
            ) 
        WHERE
            1=1 
                and connect_by_isleaf = 1 
            START WITH rn = 1 CONNECT BY RN - 1 = PRIOR RN 
            AND user_number = PRIOR user_number 

    postgresql 

    select user_number,name from 
    (
     select alias1.*,
     row_number() over (partition by alias1.user_number order by rn desc) as leaf
    from (    
    with RECURSIVE cte as
            (
            select a.user_number,cast(A.name as varchar(1000)),A.rn from (
            SELECT
                        u.user_number,
                        r.NAME,
                        ROW_NUMBER ( ) OVER ( PARTITION BY u.user_number ORDER BY ur.role_id ) RN
                    FROM
                        ems_role r,
                        ( SELECT ur.user_id, ur.role_id FROM ems_user_role ur 
                        UNION SELECT ur.user_id, ur.role_id FROM ems_user_device_role ur ) ur,
                        ems_user u 
                    WHERE
                        ur.user_id = u.ID 
                        AND r.ID = ur.role_id   ) A where a.rn = 1
            union all 
            select k.user_number,cast(c.name||','||k.name as varchar(1000)) as path,k.rn from (SELECT
                        u.user_number,
                        r.NAME,
                        ROW_NUMBER ( ) OVER ( PARTITION BY u.user_number ORDER BY ur.role_id) RN 
                    FROM
                        ems_role r,
                        ( SELECT ur.user_id, ur.role_id FROM ems_user_role ur 
                        UNION SELECT ur.user_id, ur.role_id FROM ems_user_device_role ur ) ur,
                        ems_user u 
                    WHERE
                        ur.user_id = u.ID 
                        AND r.ID = ur.role_id) k inner join cte c on c.RN + 1 = k.rn and c.user_number = k.user_number
            )
        select e.* from cte e where 1=1 
    ) as alias1 where 1=1 
    ) as  alias2 where leaf =1
  • 相关阅读:
    获取微信接口各种信息
    servlet
    springmvc上传图片,发送邮件
    linuxmint卸载软件
    linuxmint更改权限
    screen 链接远程桌面
    eclipse添加桌面快捷方式
    window精选软件
    windows Server2012 IIS8.0配置安装完整教程
    SQL Server 2012 sa 用户登录 18456 错误
  • 原文地址:https://www.cnblogs.com/acme6/p/12516041.html
Copyright © 2011-2022 走看看