Oracle中KEEP FIRST/LAST的解释

mon*_*mon 13 sql oracle

问题

请解释一下KEEP到底是什么以及有/没有它的效果。

寻找解释但找不到明确的解释。

Oracle 中带和不带 KEEP 的 PARTITION BY

“KEEP”的真正意义/力量是当您对不同列进行聚合和排序时。

保留条款

不幸的是,当您开始搜索“keep”子句时,您将在 Oracle 文档中找不到任何内容(希望通过这篇博文,人们现在可以获得参考)。当然,Oracle 记录了这些功能。您只需知道它们在 SQL 语言参考中被称为 FIRST 和 LAST 即可。

# However, you can do even better by just adding three "keep clause" functions to the original query:
SELECT
    ra.relation_id,
    MAX(ra.startdate) startdate,
    MAX(ra.address) KEEP(DENSE_RANK LAST ORDER BY ra.startdate) address,
    MAX(ra.postal_code) KEEP(DENSE_RANK LAST ORDER BY ra.startdate) postal_code,
    MAX(ra.city) KEEP(DENSE_RANK LAST ORDER BY ra.startdate) city
FROM
    relation_addresses ra
WHERE
    ra.startdate <= to_date(:reference_date, 'yyyy-mm-dd')
GROUP BY
    ra.relation_id
Run Code Online (Sandbox Code Playgroud)

MT0*_*MT0 21

在这里解释一下我的答案:

MAX(ra.address) KEEP(DENSE_RANK LAST ORDER BY ra.startdate)
Run Code Online (Sandbox Code Playgroud)

该语句可以(大致)从右到左的顺序考虑:

  • ORDER BY ra.startdate表示按别名表startdate的列对每个组内的行进行排序ra(隐式使用ASC结束顺序);然后
  • KEEP (DENSE_RANK LAST表示对每组中的那些有序行进行(连续)排名(排序列具有相同值的行将被给予相同的排名),并且KEEP仅对排名中的那些行LAST(即具有最大值的行startdate)进行排名;最后
  • MAX(ra.address)对于每组剩余保留的行,返回最大地址。

您正在从每组address具有最大值(最新)的行中查找最大值。startdate


没有该KEEP条款:

MAX(ra.startdate)
Run Code Online (Sandbox Code Playgroud)

startdate意味着找到每个组的列的最大值(最新) 。