【问题标题】:MAX Date range in SQLSQL 中的最大日期范围
【发布时间】:2013-12-12 06:37:43
【问题描述】:

多条记录的最大日期范围

我有一张如下表。

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 条记录不应存在,因为它们已更新其出版订阅期。

我怎样才能得到这些结果。请帮忙。

亲爱的,我可以获取给定日期范围的数据。但它返回 1 , 4 ,6 。我只需要记录 6 即可返回。因为我需要指定客户的最新日期范围才能发布。(每个客户每个出版物的最高日期范围)。至于我的输入(搜索 11 月的结束日期)。 1 不是必需的,因为该客户已在 2 中延长了“DLD”期限。在第 4 条记录中也是如此。第 4 个不需要,因为它在记录 3 中扩展(更新)

【问题讨论】:

  • 您是说 SQL Server 吗?你的日期真的存储为字符串吗??
  • 您使用的是哪个 DBMS?甲骨文? Postgres?
  • @a_horse_with_no_name 我正在使用 oracle
  • @Mostly 我在使用 plsql

标签: sql date numeric-limits


【解决方案1】:

您或许可以为此目的使用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

【讨论】:

  • 亲爱的,我可以获得给定日期范围内的数据。但它返回
  • 亲爱的,我可以获得给定日期范围内的数据。但它返回 1 , 4 ,6 。我只需要记录 6 即可返回。因为我需要指定客户的最新日期范围才能发布。(每个客户每个出版物的最高日期范围)。至于我的输入(搜索 11 月的结束日期)。 1 不是必需的,因为该客户已在 2 中延长了“DLD”期限。在第 4 条记录中也是如此。第 4 次不需要,因为它在记录 3 中扩展(更新)。
  • @Deepa,非常感谢。用户正在测试报告。到目前为止,它是完美的。
【解决方案2】:

在 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

【讨论】:

  • 嗨 Deepshikha,在我的情况下,有多个表。我找到了用于内部连接的表。但是在哪里放置另一张桌子?
  • 内部查询的目的是获取每个客户和给定出版物的最大日期。基于此值,外部查询从主表中获取日期在给定日期范围之间的记录。现在这取决于您的加入条件。如果您想在整个结果中添加过滤器,请在外部查询中添加与新表的连接。但是如果您想更改基本记录,请在内部查询中添加连接。
  • ---由于评论字符有限---我正在逐部分发送我的查询---如果你能帮助我。 --- PART 01 选择 hca_agent.account_number 代理代码、hca_sub.account_number 订阅者代码、hp_sub.party_name 订阅者名称、sum(xssl.quantity) 数量、msi.segment1 Publication_name、xssl.start_date Period_from、xssl.end_date Period_to------- - 我需要获取 MAX 结束日期----相关的开始日期
  • ----PART 02 from xxwnl_st_subscr_line xssl,-----主表 xxwnl_supp_temp_line xsl,----其他 xxwnl_supp_temp_header xsh,-----其他 hz_cust_accounts hca_sub,----客户----- hz_parties hp_sub,----其他 mtl_system_items_b msi,----出版-- hz_cust_accounts hca_agent,----其他 hz_parties hp_agent----其他
  • ---第 03 部分,其中 xssl.supply_line_id = xsl.supply_line_id 和 xsl.header_id = xsh.header_id 和 hca_sub.cust_account_id = xssl.subscriber_cust_account_id 和 hp_sub.party_id = hca_sub.party_id 和 msi.inventory_item_id = xsl.inventory_item_id 和 msi.organization_id = oe_sys_parameters.value('MASTER_ORGANIZATION_ID', fnd_profile.value('ORG_ID')) 和 hca_agent.cust_account_id = xsh.cust_account_id 和 hp_agent.party_id = hca_agent.party_id 和 hca_agent.customer_class_code = 'SAGENT' 和hca_agent.account_number like '95%'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-03
  • 2021-10-13
  • 1970-01-01
  • 2021-10-14
  • 1970-01-01
  • 1970-01-01
  • 2021-01-29
相关资源
最近更新 更多