如何在Oracle SQL中检索同一日期的组中的第一条记录和最后一条记录

问题描述 投票:1回答:1

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9JNzFYMi5qcGcifQ==” alt =“在此处输入图像描述”>

请建议我,我已经尝试使用Oracle SQL中的分区和组,但是无法获得正确的查询

sql oracle oracle10g oracle-sqldeveloper
1个回答
0
投票

一个选项两次使用row_number()

select primary_key, id, created_date
from (
    select
        t.*,
        row_number() over(partition by id, trunc(created_date) order by created_date  asc) rn_asc,
        row_number() over(partition by id, trunc(created_date) order by created_date desc) rn_desc
    from mytable t
) t
where 1 in (rn_asc, rn_desc)
© www.soinside.com 2019 - 2024. All rights reserved.