简体   繁体   English

Oracle SQL-比较带日期的多行

[英]Oracle SQL - Comparing Multiple Rows with Dates

I'm working in Oracle SQL. 我正在使用Oracle SQL。 Suppose I have a table that lists the following. 假设我有一个列出以下内容的表。

TABLE

PurchaseID    CustID      Location       Date  
    1            1           A        8/23/2013 12:00:00 AM  
    2            1           B        8/15/2013 12:00:00 AM  
    3            2           A        5/15/2013 12:00:00 AM  
    4            2           B        1/01/2005 12:00:00 AM  
    5            3           A        1/15/2001 12:00:00 AM  
    6            3           A        1/30/2001 12:00:00 AM  
    7            3           B        8/23/2013 12:00:00 AM  
    8            4           A        5/05/2012 12:00:00 AM
    9            4           B        8/15/2010 12:00:00 AM
    10           4           A        9/20/2008 12:00:00 AM  

I'm trying to write a query that compares the purchases by customer so that the output is every instance where a particular customer makes a purchase at two different locations within 2 years of each other. 我正在尝试编写一个查询,以比较客户的购买情况,以使输出结果是特定客户在彼此之间两年内在两个不同位置进行购买的每个实例。 I'm getting particularly tripped up on the CustID=3 and CustID=4 type cases, where there are difficult combinations of location/date. 在CustID = 3和CustID = 4类型的情况下,尤其是在位置/日期的组合很困难的情况下,我特别着迷。 The output of the query should look like this. 查询的输出应如下所示。

PurchaseID    CustID      Location       Date  
    1            1           A        8/23/2013 12:00:00 AM  
    2            1           B        8/15/2013 12:00:00 AM   
    8            4           A        5/05/2012 12:00:00 AM
    9            4           B        8/15/2010 12:00:00 AM
    10           4           A        9/20/2008 12:00:00 AM

In the Output, CustID=1's purchases are returned because they are in different locations within 2 years of each other. 在输出中,将返回CustID = 1的购买信息,因为它们在两年之内位于不同的位置。 CustID=2 are thrown out because they are not within 2 years. CustID = 2被排除在外,因为它们不在2年内。 CustID=3 has two purchases within 2 years of each other, but they are thrown out because they are in the same location. CustID = 3彼此之间有2年之内的两次购买,但由于它们位于同一位置而被丢弃。 And CustID=4's purchases are kept because Purchases 8 and 9 are within 2 years and in different locations, and 9 and 10 are within 2 years and in different locations (I want these to be kept despite 8 and 10 being in the same location and not within 10 years). CustID = 4的购买被保留,因为购买8和9在2年内且在不同的位置,而9和10在2年内且在不同的位置(尽管8和10在同一位置, (不在10年内)。

Note: The Date column has the Oracle SQL 'Date' Datatype. 注意:“日期”列具有Oracle SQL“日期”数据类型。

As always, any help/guidance would be greatly appreciated. 一如既往,任何帮助/指导将不胜感激。

You can limit the search to the cases where either the next or previous location is different from the current one. 您可以将搜索限制为下一个或上一个位置与当前位置不同的情况。 Then look at the time difference to choose the rows. 然后查看时差以选择行。

This makes heavy use of lag() and lead() : 这大量使用了lag()lead()

select t.PruchaseId, t.CustId, t.Location, t.Date
from (select t.*,
             lag(location) over (partition by CustId order by Date) as prevloc,
             lead(location) over (partition by CustId order by Date) as nextloc,
             lag(date) over (partition by CustId order by Date) as prevdate,
             lead(date) over (partition by CustId order by Date) as nextdate
      from t
     ) t
where ((prevloc <> location) and (add_months(prevdate, 2*12) > date)) or
      ((nextloc <> location) and (add_months(date, 2*12) > nextdate));

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

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