Home > Mobile >  Difference in dates when actions are taken multiple times
Difference in dates when actions are taken multiple times

Time:01-12

I have the following table:

Table (History h)
| Source ID | Action             | Created Date |
|  1        | Filing Rejected    | 1/3/2023     |
|  2        | Filing Rejected    | 1/4/2023     |
|  1        | Filing Resubmitted | 1/5/2023     |
|  3        | Filing Rejected    | 1/5/2023     |
|  2        | Filing Resubmitted | 1/6/2023     |
|  1        | Filing Rejected    | 1/7/2023     |
|  3        | Filing Resubmitted | 1/8/2023     |
|  1        | Filing Resubmitted | 1/9/2023     |

The results that I want are:

|Source ID | Rejected Date | Resubmitted Date | Difference |
|  1       | 1/3/2023      | 1/5/2023         | 2          |
|  1       | 1/7/2023      | 1/9/2023         | 2          |
|  2       | 1/4/2023      | 1/6/2023         | 2          |   
|  3       | 1/5/2023      | 1/8/2023         | 3          |   
          

My current query language is:

SELECT h1.Source_ID, min(CONVERT(varchar,h1.CREATED_DATE,101)) AS 'Rejected Date',
       min(CONVERT(varchar,h2.Created_Date,101)) AS 'Resubmitted Date',
       DATEDIFF(HOUR, h1.Created_Date, min(h2.Created_Date)) / 24 Difference
FROM History h1 INNER JOIN History h2
ON h2.Source_ID = h1.Source_ID AND h2.Created_Date > h1.Created_Date
WHERE (h1.Created_Date >= '2023-01-01 00:00:00.000' AND h1.Created_Date <= '2023-01-31 23:59:59.000') 
AND ((h1.CHANGE_VALUE_TO = 'Filing Rejected' AND h2.CHANGE_VALUE_TO = 'Filing Resubmitted'))
GROUP BY h1.Source_ID, h1.Created_Date,h2.Created_Date
ORDER BY 'Rejected Date' ASC;

The results I get are:

|Source ID | Rejected Date | Resubmitted Date | Difference |
|  1       | 1/3/2023      | 1/5/2023         | 2          |
|  1 *      | 1/3/2023      | 1/9/2023         | 6          |
|  1       | 1/7/2023      | 1/9/2023         | 2          |
|  2       | 1/4/2023      | 1/6/2023         | 2          |   
|  3       | 1/5/2023      | 1/8/2023         | 3          |   
          

So there is one row that is showing up that should not be. I have marked it with an asterisk.

I just want the difference from the first rejection to the first resubmission, the second rejection to the second rejection.

Any help, another idea on how to do it, anything really, is greatly appreciated.

CodePudding user response:

If events always properly interleave, one approach uses row_number() and conditional aggregation:

select source_id,
    max(case when action = 'Filing Rejected'    then created_date end) rejected_date,
    max(case when action = 'Filing Resubmitted' then created_date end)  resubmitted_date
from (
    select h.*,
        row_number() over(partition by source_id, action order by created_date) rn
    from history h
    where created_date >= '2023-01-01' and created_date < '2023-02-01'
) h
group by source_id, rn
order by source_id, rn

This will not work if your data has consecutive rejections or resubmissions.

As for the date difference, we can add another layer so we don’t need to type the conditional expressions twice:

select h.*, datediff(day, rejected_date, resubmitted_date) diff_in_days
from (
    select source_id,
        max(case when action = 'Filing Rejected'    then created_date end) rejected_date,
        max(case when action = 'Filing Resubmitted' then created_date end)  resubmitted_date
    from (
        select h.*,
            row_number() over(partition by source_id, action order by created_date) rn
        from history h
        where created_date >= '2023-01-01' and created_date < '2023-02-01'
    ) h
    group by source_id, rn
) h
order by source_id, rejected_date
  • Related