具有共同ID的组之间具有最大天数的两个日期之间的差异

时间:2016-10-25 21:07:59

标签: sql oracle group-by date-difference groupwise-maximum

如果我从B组中取出日期,那么我必须找到B组中任意两个日期之间的最大差异,并将days_difference显示为单独的列。还想在两个具有最大days_difference的不同列中显示这两个日期。

条件:两个类ID在数据中可能具有相同的类名。如您所见,Class-ID 1238和1252具有相同的Class-Name。所以输出应该是每个class-id

所以结果应该是:ClassID,ClassName,Date1,Date 2,DateDiff

ClassName | Dates     | ClassID  | Spend($)
ClassA    | 21-Jun-16 | 1238     | 1
ClassA    | 27-Jun-16 | 1238     | 2
ClassA    | 14-Apr-11 | 1252     | 3
ClassA    | 15-Apr-11 | 1252     | 4
ClassC    | 26-Oct-15 | 1261     | 5
ClassC    | 21-Oct-15 | 1261     | 6
ClassA    | 10-Dec-10 | 1252     | 21
ClassC    | 15-Dec-14 | 1261     | 23
ClassC    | 15-May-14 | 1261     | 34
ClassA    | 10-Jan-09 | 1252     | 54
ClassA    | 12-Jun-09 | 1238     | 65
ClassA    | 29-Jun-12 | 1238     | 76
ClassA    | 11-Apr-08 | 1252     | 87
ClassA    | 01-Apr-16 | 1252     | 56
ClassC    | 16-Oct-16 | 1261     | 43
ClassC    | 02-Sep-05 | 1261     | 33
ClassA    | 29-Dec-13 | 1252     | 77
ClassC    | 13-Dec-15 | 1261     | 87
ClassC    | 13-Aug-10 | 1261     | 98
ClassA    | 21-Jan-14 | 1252     | 12

2 个答案:

答案 0 :(得分:0)

您可以使用LEAD或LAG获取连续日期之间的日差。

SELECT 
    Dates,
    LEAD(Dates) OVER (PARTITION BY ClassID ORDER BY Dates) NextDate,
    Dates - LEAD(Dates) OVER (PARTITION BY ClassID ORDER BY Dates) DaysDiff
FROM MyTable

通过获取具有最高DaysDiff值的记录

,您可以从此结果中获得所需的结果

for reference

对结果进行排名,并获得每个classID排名最高的日期差异。

SELECT * FROM (
    SELECT *, RANK() OVER (PARTITION BY ClassID ORDER BY DaysDiff DESC) Rnk
    FROM (
        SELECT 
            ClassID,
            ClassName,
            Dates,
            LEAD(Dates) OVER (PARTITION BY ClassID ORDER BY Dates) NextDate,
            Dates - LEAD(Dates) OVER (PARTITION BY ClassID ORDER BY Dates) DaysDiff
        FROM MyTable) t1
    ) t2
WHERE t2.Rnk = 1

上述样本数据的输出应为。

ClassName ClassID     Dates                   NextDate                DaysDiff    Rnk
--------- ----------- ----------------------- ----------------------- ----------- --------------------
ClassA    1238        2012-06-29 00:00:00.000 2016-06-21 00:00:00.000 1453        1
ClassA    1252        2011-04-15 00:00:00.000 2013-12-29 00:00:00.000 989         1
ClassC    1261        2005-09-02 00:00:00.000 2010-08-13 00:00:00.000 1806        1

答案 1 :(得分:0)

SELECT A.ClassId,
       B.LagDates,
       B.Dates 
  FROM 
     (
         SELECT ClassId,
                MAX( DaysDiff ) AS MD
           FROM
              (       
                SELECT *,
                       LAG( Dates, 1 ) OVER ( PARTITION BY ClassID ORDER BY Dates ) AS LagDates,   
                       TO_DATE( Dates, 'YYYY-MM-DD' ) 
                       -
                       LAG( Dates, 1 ) OVER ( PARTITION BY ClassID ORDER BY    
                       Dates ) AS DaysDiff
                  FROM ( SELECT *                                    
                           FROM Table
                          ORDER BY ClassId, Dates 
                        ) 
               ) 
         GROUP BY ClassId
      ) A,
      (
          SELECT *,
                 LAG( Dates, 1 ) OVER ( PARTITION BY ClassID ORDER BY Dates ) AS LagDates, 
                 TO_DATE( Dates, 'YYYY-MM-DD' ) 
                 -
                 LAG( Dates, 1 ) OVER ( PARTITION BY ClassID ORDER BY Dates ) 
                 AS DaysDiff
            FROM (
                    SELECT * 
                      FROM Table
                     ORDER BY ClassId, Dates  
                  ) 
       ) B
WHERE A.ClassId = B.ClassId
  AND A.MD = B.DaysDiff;