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

时间:2014-03-14 12:29:00

标签: sql oracle11g query-tuning

如何从以下查询中更有效,更优雅地获取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;

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您需要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;

ALL CAPS并不能真正传达你想要做的事情。