提问人:lingaiah 提问时间:8/14/2023 最后编辑:lingaiah 更新时间:8/14/2023 访问量:25
SQL Server 最小和最大日期介于 2 列和多行之间
Sql server min and max dates between 2 columns and multiple rows
问:
在 Sqlserver 2019 脚本中, 我有一个员工休假表,如下所示 例如,员工已用完年假天数,如下所述
Empcode LeaveFrom LeaveTo
A 23/07/2023 01/08/2023
02/08/2023 03/08/2023
04/08/2023 04/08/2023
B 10/07/2023 01/08/2023
02/08/2023 03/08/2023
预期输出结果
Ecode 请假至 A 23/07/2023 04/08/2023
B 10/07/2023 03/08/2023
我已经用过了,但我仍然没有得到我预期的结果。寻找建议。lead function
我试过了lead function, row_number(), rank, dense_rank
答:
0赞
Qasim Mughal
8/14/2023
#1
我没有找到你,但你可以试试这个:)
SELECT Empcode, MIN(LeaveDate) AS MinLeaveDate, MAX(LeaveDate) AS MaxLeaveDate
FROM (
SELECT Empcode, LeaveFrom AS LeaveDate FROM Leaves
UNION
SELECT Empcode, LeaveTo AS LeaveDate FROM Leaves
) AS CombinedLeaves
GROUP BY Empcode;
评论