0

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

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

2 回答 2

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 值最高的记录从此结果中得到您想要的结果

以供参考

对结果进行排名并获得每个 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
于 2016-10-25T21:23:17.950 回答
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;
于 2016-10-25T21:26:15.843 回答