简体   繁体   English

SQL Server 查询返回两行中的日期与 OUTPUT 中的初始状态列之间的差异

[英]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 and history_perf_rat

  • current_perf_rat with columns PersonRatingID, FirstName, LastName, Email, CurrentStatusID current_perf_rat包含 PersonRatingID、FirstName、LastName、Email、CurrentStatusID 列
  • history_perf_rat with columns PersonRatingID, StatusID, StatusName, StatusTimestamp, isCurrent history_perf_rat包含 PersonRatingID、StatusID、StatusName、StatusTimestamp、isCurrent 列

current_perf_rat current_perf_rat

PersonRatingID人员评级ID FirstName LastName Email电子邮件 CurrentStatusID当前状态ID
123 123 XZY许愿 HUG拥抱 xyz@gmail.com xyz@gmail.com 678 678
456 456 smith史密斯 john约翰 smith @xyz.com史密斯@xyz.com 746 746

history_perf_rat: history_perf_rat:

PersonRatingID人员评级ID StatusID状态ID StatusName状态名称 StatusTimestamp状态时间戳 isCurrent是当前
123 123 678 678 Rating Given给出的评级 2021-10-01 10:00 2021-10-01 10:00 Y
123 123 746 746 Rating in Draft草稿评级 2021-09-28 15:00 2021-09-28 15:00 N N
123 123 567 567 Started Rating开始评分 2021-09-01 02:00 2021-09-01 02:00 N N
456 456 678 678 Rating Given给出的评级 2021-08-13 22:00 2021-08-13 22:00 Y
456 456 746 746 Rating in Draft草稿评级 2021-09-17 15:00 2021-09-17 15:00 N N
456 456 567 567 Started Rating开始评分 2021-08-01 02:00 2021-08-01 02:00 N N
456 456 561 561 Initiated Rating初始评级 2020-08-01 02:00 2020-08-01 02:00 N 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.我想创建一个查询,以了解仅在 2021 年,应用程序从所有人员评级 ID 的第一个状态移动到下一个状态需要多长时间。

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人员评级ID StatusName状态名称 Application_date申请日期 rn
456 456 Started Rating开始评分 2021-08-01 2021-08-01 2 2
456 456 Initiated Rating初始评级 2020-08-01 2020-08-01 1 1
123 123 Rating in Draft草稿评级 2021-09-28 2021-09-28 2 2
123 123 Started Rating开始评分 2021-09-01 2021-09-01 1 1

Expected output:预期输出:

PersonRatingID人员评级ID StatusName状态名称 Time_Taken花的时间
456 456 Initiated Rating初始评级 1 Year 1年
123 123 Started Rating开始评分 1 year 28 days 1年28天
  • 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 ?如何修改这个 SQL 语句来获取这两个日期和初始状态名称之间的差异,即“Initiated Rating”和 started rating ? Is there a way to do it by using LAG or something?有没有办法通过使用 LAG 或其他方法来做到这一点?

  • 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 ?例如,在上面 456 的结果中,上面也包含了 2020 时间戳,但我只想包含 2021。这可以动态完成吗?

You can use ROW_NUMBER to only get the first row, and LEAD to get the next Timestamp after the first您可以使用ROW_NUMBER仅获取第一行,并使用LEAD获取第一行之后的下一个Timestamp

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 数据库<>小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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