簡體   English   中英

如何根據日期查找每個 id 的最后一個復合鍵

[英]how to find last composite key for each id based on date

我正在使用 Oracle 開發人員,

並想為我的表“bookborrow”找到每個 id 的最后一條記錄,它有composite keyiddateofborrow是主鍵(復合鍵)

列:

id  |  studname  |  dateofborrow  |  bookname    
----+------------+----------------+------------------------
1   |  *Scotty*  |  10-OCT-05     |  DB Admin      
1   |  *Scotty*  |  16-NOV-05     |  Database Security    
1   |  *Scotty*  |  09-DEC-06     |  sql server 2005   
2   |  *Andy*    |  30-MAR-04     |  Math  
2   |  *Andy*    |  14-JUN-06     |  Calculation of matrix   
3   |  *Jack*    |  29-JAN-09     |  physics   

如何像這樣根據dateofborrow output 查找每個 id 的最后一條記錄?

id  |  studname  |  dateofborrow  |  bookname    
----+------------+----------------+------------------------
1   |  Scotty    |  09-DEC-06     |  sql server 2005     
2   |  Andy      |  14-JUN-06     |  Calculation of matrix     
3   |  Jack      |  29-JAN-09     |  physics     

有什么幫助嗎?

你需要什么可以通過太多方式實現,這里有兩個

with my_data_set as (
            select 1 as id,'Scotty' as studname , '10-OCT-05' as dateofborrow,'DB Admin' as bookname from dual union
            select 1 as id,'Scotty' as studname , '16-NOV-05' as dateofborrow,'Database Security' as bookname from dual union
            select 1 as id,'Scotty' as studname , '09-DEC-06' as dateofborrow,'sql server 2005' as bookname from dual union
            select 2 as id,'Andy' as studname , '30-MAR-04' as dateofborrow,'Math' as bookname from dual union
            select 2 as id,'Andy' as studname , '14-JUN-06' as dateofborrow,'Calculation of matrix' as bookname from dual union
            select 3 as id,'Jack' as studname , '29-JAN-09' as dateofborrow,'physics' as bookname from dual
           ) 
           select * from my_data_set a
           where a.dateofborrow = (select max(to_date(dateofborrow,'DD-MONTH-YY')) from my_data_set where a.id = id);


with my_data_set as (
            select 1 as id,'Scotty' as studname , '10-OCT-05' as dateofborrow,'DB Admin' as bookname from dual union
            select 1 as id,'Scotty' as studname , '16-NOV-05' as dateofborrow,'Database Security' as bookname from dual union
            select 1 as id,'Scotty' as studname , '09-DEC-06' as dateofborrow,'sql server 2005' as bookname from dual union
            select 2 as id,'Andy' as studname , '30-MAR-04' as dateofborrow,'Math' as bookname from dual union
            select 2 as id,'Andy' as studname , '14-JUN-06' as dateofborrow,'Calculation of matrix' as bookname from dual union
            select 3 as id,'Jack' as studname , '29-JAN-09' as dateofborrow,'physics' as bookname from dual
           ) 
           select * from (
           select a.*,row_number() over (partition by id order by to_date(dateofborrow,'DD-MONTH-YY') desc) r  from my_data_set a
           ) where r =1;

請使用以下查詢。 您必須使用row_number()分析 function

select id,studname,dateofborrow,bookname from 
(select id,studname,dateofborrow,bookname,row_number() over(partition by studname order 
by dateofborrow desc) as rnk
from table_name) qry where rnk = 1; 

暫無
暫無

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

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