Dense_rank first Oracle to Postgresql convert

泪湿孤枕 提交于 2019-12-11 17:03:08

问题


I'm trying to convert the following Oracle query into Postgresql. I could convert the rest of the blocks, the problem is I don't know how to convert this block:

    SELECT ai.uid
        ,max(ai.OWNER) KEEP (
            dense_rank first ORDER BY ai.AGENT_ID DESC
            ) AS OWNER
        ,max(ai.EMPLOYEE_KEY) KEEP (
            dense_rank first ORDER BY ai.AGENT_ID DESC
            ) AS EMPLOYEE_KEY
        ,max(ai.MANAGER_LOGIN) KEEP (
            dense_rank first ORDER BY ai.AGENT_ID DESC
            ) AS MANAGER
        ,max(ai.CALL_CENTER_NAME) KEEP (
            dense_rank first ORDER BY ai.AGENT_ID DESC
            ) AS CALL_CENTER_NAME
        ,max(ai.CITY) KEEP (
            dense_rank first ORDER BY ai.AGENT_ID DESC
            ) AS CITY

    FROM agent_info ai

    WHERE translate(ai.UID, 'X0123456789', 'X') IS NULL

    GROUP BY ai.UID

回答1:


I think you can convert it to contain a single DENSE_RANK() and do a MAX of all on the first record.

SELECT UID,
       MAX(OWNER),
       MAX(EMPLOYEE_KEY),
       MAX(MANAGER),
       MAX(CALL_CENTER_NAME),
       MAX(CITY)
  FROM (SELECT ai.UID,
               ai.OWNER AS OWNER,
               ai.EMPLOYEE_KEY AS EMPLOYEE_KEY,
               ai.MANAGER_LOGIN AS MANAGER,
               ai.CALL_CENTER_NAME AS CALL_CENTER_NAME,
               ai.CITY AS CITY,
               DENSE_RANK () OVER (PARTITION BY ai.UID ORDER BY ai.AGENT_ID DESC) rnk
          FROM agent_info ai
         WHERE TRANSLATE (ai.UID, 'X0123456789', 'X') IS NULL)
 WHERE rnk = 1
GROUP BY UID;


来源:https://stackoverflow.com/questions/48638845/dense-rank-first-oracle-to-postgresql-convert

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