2013-12-12 50 views
-1

MAX來自多個記錄的日期範圍MAX在SQL中的日期範圍

我有一張如下所示的表格。

Customer Publication Start Date End Date 
1 S0048 DLD   01-JAN-2013 15-NOV-2013 
2 S0048 DLD   03-MAR-2013 31-DEC-2013 
3 S0048 SLD   01-FEB-2013 31-DEC-2013 
4 S0048 SLD   01-FEB-2013 30-NOV-2013 
5 S0145 DLD   01-JAN-2013 01-MAR-2013 
6 S0145 DLD   02-FEB-2013 28-NOV-2013 

我需要通過給出結束日期範圍來得到結果。 例如:如果輸入:end date 01-NOV-2013 to 30-NOV-2013(搜索誰是11月一個月到期)

結果應該是

S0145 DLD   02-FEB-2013 28-NOV-2013 

請注意,第一和第四的記錄不應該出現,因爲他們有更新其出版認購期。

我怎樣才能得到這些結果。請幫忙。

親愛的所有人,我可以得到給定日期範圍的數據。但它返回1,4,6。我只需要記錄6即可獲得回報。因爲我需要某個出版物的特定客戶的最新日期範圍(每個客戶每個出版物的最高日期範圍)。至於我的輸入(搜索11月份結束日期)。因爲該客戶已經延長了「DLD」期限,因此1沒有必要。第4條記錄也是如此。第四不需要,延續(更新)中記錄3

+0

你的意思是SQL Server的?你的日期真的存儲爲一個字符串? –

+0

您正在使用哪些DBMS?甲骨文? Postgres的? –

+0

@ a_horse_with_no_name我正在使用oracle – user3093883

回答

0

你很可能STR_TO_DATE用於此目的:

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_str-to-date

例如:

SELECT MAX(STR_TO_DATE(`Start Date`, '%d-%b-%Y')) FROM customers 

的格式說明(%d等)在這裏列出:

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format

+0

親愛的所有人,我可以獲取給定日期範圍內的數據。但它返回 – user3093883

+0

親愛的所有人,我可以得到給定日期範圍的數據。但它返回1,4,6。我只需要記錄6即可獲得回報。因爲我需要某個出版物的特定客戶的最新日期範圍(每個客戶每個出版物的最高日期範圍)。至於我的輸入(搜索11月份結束日期)。因爲該客戶已經延長了「DLD」期限,因此1沒有必要。第4條記錄也是如此。 4th不需要,因爲它擴大(更新)在記錄3中。 – user3093883

+0

@Deepa,Thankx很多。用戶正在測試報告。到目前爲止它是完美的。 – user3093883

0

在TSQL可以編寫一個查詢爲:

SELECT T2.customer, 
     T2.publication, 
     T2.startdate, 
     T2.enddate 
FROM table1 T2 
     INNER JOIN (SELECT t1.customer, 
          Max(t1.enddate) AS MaxEnddate 
        FROM table1 T1 
        GROUP BY t1.customer, 
          T1.publication) T 
       ON T.customer = T2.customer 
        AND T.maxenddate = T2.enddate 
WHERE T2.enddate BETWEEN '01-NOV-2013' AND '30-NOV-2013' 

更新:

select hca_agent.account_number agency_code, 
     hca_sub.account_number subscriber_code, 
     hp_sub.party_name subscriber_name, 
     sum(xssl.quantity) qty, 
     msi.segment1 Publication_name, 
     xssl.start_date Period_from, 
     xssl.end_date Period_to-------- I need to get the MAX end date----with the relevant start date 
     from xxwnl_st_subscr_line xssl 
     inner join 
     (
     select xssl_inner.subscriber_cust_account_id, 
       MAX(xssl_inner.end_date) as MaxEnddate 
     from xxwnl_st_subscr_line xssl_inner 
     group by xssl_inner.subscriber_cust_account_id) T on 
     T.subscriber_cust_account_id = xssl.subscriber_cust_account_id 
     and T.MaxEnddate = xssl.end_date, 
     xxwnl_supp_temp_line xsl, ----others 
     xxwnl_supp_temp_header xsh,-----others 
     hz_cust_accounts hca_sub,----for customer----- 
     hz_parties hp_sub,----others 
     mtl_system_items_b msi,----for publication----- 
     hz_cust_accounts hca_agent,----others 
     hz_parties hp_agent----others 
     where xssl.supply_line_id = xsl.supply_line_id 
     and xsl.header_id = xsh.header_id 
     and hca_sub.cust_account_id = xssl.subscriber_cust_account_id 
     and hp_sub.party_id = hca_sub.party_id 
     and msi.inventory_item_id = xsl.inventory_item_id 
     and msi.organization_id = oe_sys_parameters.value('MASTER_ORGANIZATION_ID', fnd_profile.value('ORG_ID')) 
     and hca_agent.cust_account_id = xsh.cust_account_id 
     and hp_agent.party_id = hca_agent.party_id 
     and hca_agent.customer_class_code = 'SAGENT' and hca_agent.account_number like '95%' 
     and xssl.end_date between TO_DATE('&FROM_DATE','DD/MON/RRRR') 
     AND TO_DATE('&TO_DATE','DD/MON/RRRR') 
     group by hca_agent.account_number , hca_sub.account_number , hp_sub.party_name , msi.segment1 , xssl.start_date , xssl.end_date 
     order by 1,2,3 
+0

嗨Deepshikha,在我的情況下,有不止一個table.I找到內部連接的表。但在哪裏放置其他表? – user3093883

+0

內部查詢的目標是爲每個客戶和給定的出版物獲取最大日期。根據此值,外部查詢從主表中獲取記錄,其中日期位於給定日期範圍之間。現在取決於你的加入條件。如果您想要將過濾器添加到整個結果中,則在外部查詢中添加與新表的連接。但是,如果要更改基本記錄,請將連接添加到內部查詢中。 – Deepshikha

+0

---由於評論有限字符---我正在發送我的查詢部分---如果你能請幫助我。 --- PART 01 選擇hca_agent.account_number agency_code, hca_sub.account_number subscriber_code, hp_sub.party_name subscriber_name, 總和(xssl.quantity)數量, msi.segment1 PUBLICATION_NAME, xssl.start_date Period_from, xssl.end_date Period_to --------我需要獲得MAX結束日期----相關開始日期 – user3093883