[英]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![]() |
FirstName![]() |
LastName![]() |
Email![]() |
CurrentStatusID![]() |
---|---|---|---|---|
123 ![]() |
XZY![]() |
HUG![]() |
xyz@gmail.com ![]() |
678 ![]() |
456 ![]() |
smith![]() |
john![]() |
smith @xyz.com![]() |
746 ![]() |
history_perf_rat: 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.我想创建一个查询,以了解仅在 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![]() |
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 ?如何修改这个 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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.