【发布时间】:2023-03-26 04:54:02
【问题描述】:
如何仅从以下查询的结果中获取第一行。我需要每个日期的最新记录,所以我按 created_date 进行了分区。但是在某些地方,我得到了相同的行号并且无法获得预期的输出。请找到以下查询、当前输出和预期输出。
需要进行哪些更改才能获得预期的输出?谢谢。
WITH ctetable
AS (
SELECT created_date BPMDate
,tenor
,row_number() OVER (
PARTITION BY created_date ORDER BY created_date DESC
) rw
FROM table1 a
INNER JOIN table2 b ON a.case_id = b.case_id
AND a.eligible_transaction = 'true'
AND to_date(a.created_date) >= '2020-10-01'
AND to_date(a.created_date) <= '2020-10-05'
AND case_status = 'Completed'
)
SELECT BPMDate
,Tenor
,rw
FROM ctetable
当前输出:
date tenor rw
2020-10-05 13:24:15.0 1W 1
2020-10-05 12:15:43.0 1Y 1
2020-10-05 12:15:43.0 1Y 2
2020-10-01 13:30:59.0 1W 1
2020-10-01 13:30:59.0 1W 2
预期输出:
date tenor rw
2020-10-05 13:24:15.0 1W 1
2020-10-01 13:30:59.0 1W 1
问候, 维雷什
【问题讨论】:
-
用您正在使用的数据库标记您的问题。
标签: sql datetime where-clause greatest-n-per-group