簡體   English   中英

SQL (Oracle) 查詢具有最大日期的記錄,僅當 end_dt 有值時

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

我試圖通過查看開始日期和結束日期來從一行中選擇一條記錄。 我需要做的是選擇最大開始日期,然后如果結束日期有值,則只返回該最大日期的結果。

我希望下面的圖片有助於進一步澄清這一點。 這是在基於 Oracle 的 SQL 中。

在此處輸入圖片說明

示例#2

在此處輸入圖片說明

到目前為止,我可以返回所有記錄或錯誤地返回場景 #2 中的記錄,但我還沒有找到使這項工作的最佳方法。 我將不勝感激的任何援助。

謝謝!

我會使用一個分析函數:

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

這將是最簡單的方法之一。

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

想法是得到最大的start_dt和對應的end_dt。 如果 end_dt 為空,則過濾結果。

SQL小提琴

數據庫架構

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

樣品 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');

樣本 1 的輸出

No rows

樣品 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');

樣本 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

我認為這也可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM