简体   繁体   中英

SQL Server query to return difference between dates in two rows and initial status column in OUTPUT

I have two tables, current_perf_rat and history_perf_rat

  • current_perf_rat with columns PersonRatingID, FirstName, LastName, Email, CurrentStatusID
  • history_perf_rat with columns PersonRatingID, StatusID, StatusName, StatusTimestamp, isCurrent

current_perf_rat

PersonRatingID FirstName LastName Email CurrentStatusID
123 XZY HUG xyz@gmail.com 678
456 smith john smith @xyz.com 746

history_perf_rat:

PersonRatingID StatusID StatusName StatusTimestamp isCurrent
123 678 Rating Given 2021-10-01 10:00 Y
123 746 Rating in Draft 2021-09-28 15:00 N
123 567 Started Rating 2021-09-01 02:00 N
456 678 Rating Given 2021-08-13 22:00 Y
456 746 Rating in Draft 2021-09-17 15:00 N
456 567 Started Rating 2021-08-01 02:00 N
456 561 Initiated Rating 2020-08-01 02:00 N

I want to create a query to find out how much time does it take for an application to move from the very first status to next status for all the Person Rating Ids in year 2021 only.

This below query gives me the row details and their row num -

SELECT DISTINCT
    current_perf_rat.PersonRatingID,
    history_perf_rat.StatusName,
    CAST(history_perf_rat.StatusTimestamp AS DATE) Application_date,
    ROW_NUMBER() OVER (ORDER BY history_perf_rat.StatusTimestamp ASC) rn
FROM 
    current_perf_rat, history_perf_rat
WHERE 
    current_perf_rat.PersonRatingID  = history_perf_rat .PersonRatingID 
    --and current_perf_rat.PersonRatingID =123 -- only one rating
ORDER BY
    history_perf_rat.StatusTimestamp ASC

This gives me the output I want to work with -

PersonRatingID StatusName Application_date rn
456 Started Rating 2021-08-01 2
456 Initiated Rating 2020-08-01 1
123 Rating in Draft 2021-09-28 2
123 Started Rating 2021-09-01 1

Expected output:

PersonRatingID StatusName Time_Taken
456 Initiated Rating 1 Year
123 Started Rating 1 year 28 days
  • How to modify this SQL statement to get the difference between these two dates and the initial status name ie "Initiated Rating" and started rating ? Is there a way to do it by using LAG or something?

  • Also, How to add a condition(Parameter) so that I Can chose the year for which this should be done ?

For example, in the above result for 456, 2020 timestamp is also included above but I only want to include 2021. Can this me done dynamically ?

You can use ROW_NUMBER to only get the first row, and LEAD to get the next Timestamp after the first

SELECT
  h.PersonRatingID,
  h.StatusName,
  Time_Taken = DATEDIFF(day, StatusTimestamp, NextTime)
FROM (
    SELECT *,
      NextTime = LEAD(h.StatusTimestamp)
                        OVER (PARTITION BY h.PersonRatingID ORDER BY h.StatusTimestamp),
      rn = ROW_NUMBER() OVER (PARTITION BY h.PersonRatingID ORDER BY h.StatusTimestamp)
    FROM history_perf_rat h
    WHERE h.StatusTimestamp >= '2021-01-01'
) h
WHERE h.rn = 1;

db<>fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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