如何获取未分组列上的最新插入值



如何以更有效和优雅的方式从下面的查询中获取MY_LATEST_INSERTED_NUMBER_BASING_ON_MY_DATE_COLUMN_FOR_ID_CONTEXT ?

SELECT MAX(MY_DATE_COLUMN),
 ID,
 listagg(SOME_COLUMN, '') within GROUP (ORDER BY MY_DATE_COLUMN),
 SUBSTR(
      listagg(MY_NUMBER, ';') within GROUP (ORDER BY MY_DATE_COLUMN DESC),
      1,
      instr(
             listagg(MY_NUMBER, ';') within GROUP (ORDER BY MY_DATE_COLUMN DESC),'-') - 1
           ) as MY_LATEST_INSERTED_NUMBER_BASING_ON_MY_DATE_COLUMN_IN_ID_CONTEXT
 FROM MY_TABLE
 GROUP BY ID;

如果我理解正确的话,您想要keep:

SELECT MAX(MY_DATE_COLUMN), ID,
       listagg(SOME_COLUMN, '') within GROUP (ORDER BY MY_DATE_COLUMN),
       max(MY_LATEST_INSERTED_NUMBER_BASING_ON_MY_DATE_COLUMN_IN_ID_CONTEXT)
           keep (dense_rank first order by MY_DATE_COLUMN DESC)
FROM MY_TABLE
GROUP BY ID;

相关内容

  • 没有找到相关文章

最新更新