简体   繁体   English

SQL (Oracle) 查询具有最大日期的记录,仅当 end_dt 有值时

[英]SQL (Oracle) to query for record with max date, only if the end_dt has a value

I am trying to select a record from a row by looking at both the start date and the end date.我试图通过查看开始日期和结束日期来从一行中选择一条记录。 What I need to do is pick the max start date, then only return a result from that max date if the end date has a value.我需要做的是选择最大开始日期,然后如果结束日期有值,则只返回该最大日期的结果。

I hope the images below help clarify this a bit more.我希望下面的图片有助于进一步澄清这一点。 This is in Oracle based SQL.这是在基于 Oracle 的 SQL 中。

在此处输入图片说明

Example #2示例#2

在此处输入图片说明

I can, so far, either return all the records or incorrectly return a record in scenario #2 but I've yet to figure out the best way to make this work.到目前为止,我可以返回所有记录或错误地返回场景 #2 中的记录,但我还没有找到使这项工作的最佳方法。 I would greatly appreciate any assistance.我将不胜感激的任何援助。

Thank you!谢谢!

I would use an analytic function:我会使用一个分析函数:

with sample_data as (select 1 id, 1 grp_id, to_date('01/01/2015', 'dd/mm/yyyy') st_dt, to_date('23/01/2015', 'dd/mm/yyyy') ed_dt from dual union all
                     select 2 id, 1 grp_id, to_date('24/02/2015', 'dd/mm/yyyy') st_dt, to_date('15/02/2015', 'dd/mm/yyyy') ed_dt from dual union all
                     select 3 id, 1 grp_id, to_date('17/03/2015', 'dd/mm/yyyy') st_dt, to_date('30/03/2015', 'dd/mm/yyyy') ed_dt from dual union all
                     select 4 id, 2 grp_id, to_date('01/01/2015', 'dd/mm/yyyy') st_dt, to_date('17/01/2015', 'dd/mm/yyyy') ed_dt from dual union all
                     select 5 id, 2 grp_id, to_date('21/01/2015', 'dd/mm/yyyy') st_dt, to_date('23/03/2015', 'dd/mm/yyyy') ed_dt from dual union all
                     select 6 id, 2 grp_id, to_date('14/04/2015', 'dd/mm/yyyy') st_dt, to_date('16/05/2015', 'dd/mm/yyyy') ed_dt from dual union all
                     select 7 id, 2 grp_id, to_date('28/05/2015', 'dd/mm/yyyy') st_dt, null ed_dt from dual),
             res as (select id,
                            grp_id,
                            st_dt,
                            ed_dt,
                            max(st_dt) over (partition by grp_id) max_st_dt
                     from   sample_data)
select id,
       grp_id,
       st_dt,
       ed_dt
from   res
where  st_dt = max_st_dt
and    ed_dt is not null;


        ID     GRP_ID ST_DT      ED_DT     
---------- ---------- ---------- ----------
         3          1 17/03/2015 30/03/2015

This would be one of the simplest way.这将是最简单的方法之一。

select * from
(
select apay_id,
       max(start_dt) OVER () max_start_dt,
        start_dt,
       end_dt
from sample
)
where
start_dt=max_start_dt
and end_dt is not null

Idea is to get maximum start_dt and corresponding end_dt.想法是得到最大的start_dt和对应的end_dt。 And then filter result if end_dt is null.如果 end_dt 为空,则过滤结果。

SQL Fiddle SQL小提琴

Database Schema数据库架构

create table sample
(apay_id number(7),
 account_number number(7),
 start_dt date,
 end_dt date);

Sample1样品 1

 insert into sample values(554433, 123456, '15-Aug-15', null);
 insert into sample values(112266, 123456, '21-Jul-15', '31-Aug-15');
 insert into sample values(733221, 123456, '29-Jun-15', '31-Jul-15');

Output for Sample1样本 1 的输出

No rows

Sample2样品 2

 insert into sample values(554433, 123456, '15-Aug-15', '11-Nov-15');
 insert into sample values(112266, 123456, '21-Jul-15', '31-Aug-15');
 insert into sample values(733221, 123456, '29-Jun-15', '31-Jul-15');

Output for Sample2样本 2 的输出

| APAY_ID |             MAX_START_DT |                     END_DT |
|---------|--------------------------|----------------------------|
|  554433 | August, 15 2015 00:00:00 | November, 11 2015 00:00:00 |

select * from ( select apay_id from sample where end_dt is not null order by start_dt desc) where rownum=1 select * from ( select apay_id from sample where end_dt is not null order by start_dt desc) where rownum=1

I think this can also work.我认为这也可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM