如果范围大于365天,如何比较同一组中的SQL日期范围并输出

时间:2019-06-19 18:07:21

标签: sql sql-server

Sample data - please check this iamge

我需要帮助才能从大型数据集中获得结果,因为大型数据集之间存在年龄差异。

我尝试使用一些join语句,但无法进入循环。

”逻辑: 1.从同一CUSTOMERID打印第一年(PROCDATE)。 2.如果年份(PROCDATE)相差超过365天(从剩余的行,而不是已经打印的行)中打印,则打印“”

2 个答案:

答案 0 :(得分:0)

请尝试使用以下代码段。

SELECT a.customericn, a.customerid, a.procdate,
    CASE WHEN DATEDIFF(day, a.procdate, c.procdate) > 365 THEN a.procdate
    WHEN DATEDIFF(day, a.procdate, c.procdate) = 0 THEN a.procdate
    END as RequiredOutput  
FROM TableName AS a
INNER JOIN (
        SELECT b.customericn, b.customerid, MIN(b.procdate) AS procdate
        FROM TableName AS b
        GROUP BY b.customericn, b.customerid
        ) AS c ON a.customericn = c.customericn and a.customerid = c.customerid

答案 1 :(得分:0)

在这种情况下,您可以使用滞后功能。

请找到以下查询,该查询会为您提供预期的结果。

SELECT CUSTOMERICN,
       CUSTOMERID,
       PROCDATE,
       PROCDATE - PREVIOUSDATE DIFFERENCE_DAYS,
       CASE
         WHEN PROCDATE - PREVIOUSDATE > 365 THEN
          PROCDATE
         WHEN PREVIOUSDATE IS NULL THEN
          PROCDATE
         ELSE
          NULL
       END RESULTDATE
  FROM (WITH TEMP AS(

SELECT '10001213' CUSTOMERICN ,'282212'  CUSTOMERID,TO_DATE('01/08/2003','MM/DD/YYYY')   PROCDATE FROM DUAL UNION
SELECT '10001213','282212',TO_DATE('07/26/2007','MM/DD/YYYY') FROM DUAL UNION
SELECT '10001213','282212',TO_DATE('02/04/2002','MM/DD/YYYY') FROM DUAL UNION
SELECT '10001215','244563',TO_DATE('04/11/2012','MM/DD/YYYY') FROM DUAL UNION
SELECT '10001219','279379',TO_DATE('11/26/2007','MM/DD/YYYY') FROM DUAL UNION
SELECT '10001219','279379',TO_DATE('06/16/2008','MM/DD/YYYY') FROM DUAL UNION
SELECT '10001219','279379',TO_DATE('04/28/2011','MM/DD/YYYY') FROM DUAL UNION
SELECT '10001219','279379',TO_DATE('01/14/2013','MM/DD/YYYY') FROM DUAL UNION
SELECT '10006912','256568',TO_DATE('10/12/2007','MM/DD/YYYY') FROM DUAL UNION
SELECT '10069018','257703',TO_DATE('09/10/2001','MM/DD/YYYY') FROM DUAL UNION
SELECT '10069018','257703',TO_DATE('01/29/2008','MM/DD/YYYY') FROM DUAL UNION
SELECT '10069018','257703',TO_DATE('06/12/2008','MM/DD/YYYY') FROM DUAL UNION
SELECT '10069018','257703',TO_DATE('09/24/2008','MM/DD/YYYY') FROM DUAL UNION
SELECT '10069018','257703',TO_DATE('10/24/2008','MM/DD/YYYY') FROM DUAL UNION
SELECT '10069018','257703',TO_DATE('11/17/2008','MM/DD/YYYY') FROM DUAL UNION
SELECT '10006403','255643',TO_DATE('06/29/2001','MM/DD/YYYY') FROM DUAL UNION
SELECT '10006403','255643',TO_DATE('09/09/2001','MM/DD/YYYY') FROM DUAL
  )
         SELECT CUSTOMERICN,
                CUSTOMERID,
                PROCDATE,
                LAG(PROCDATE, 1) OVER(PARTITION BY CUSTOMERID ORDER BY PROCDATE) PREVIOUSDATE
           FROM TEMP
          ORDER BY CUSTOMERID, PROCDATE)
          ORDER BY 1, 2, 3

在这里,我为每个客户使用滞后函数得出前一个日期。然后,您可以放置​​其余的逻辑(小于365的天数,等等)

结果如下。

CUSTOMERICN CUSTOMERID  PROCDATE    DIFFERENCE_DAYS RESULTDATE
10001213    282212      2/4/2002                    2/4/2002
10001213    282212      1/8/2003               338      
10001213    282212      7/26/2007              1660 7/26/2007
10001215    244563      4/11/2012                   4/11/2012
10001219    279379      11/26/2007                  11/26/2007
10001219    279379      6/16/2008              203          
10001219    279379      4/28/2011              1046 4/28/2011
10001219    279379      1/14/2013              627  1/14/2013
10006403    255643      6/29/2001                   6/29/2001
10006403    255643      9/9/2001               72           
10006912    256568      10/12/2007                  10/12/2007
10069018    257703      9/10/2001                   9/10/2001
10069018    257703      1/29/2008              2332 1/29/2008
10069018    257703      6/12/2008              135          
10069018    257703      9/24/2008              104          
10069018    257703      10/24/2008             30           
10069018    257703      11/17/2008             24