SQL Server 2016 max in partition by select 语句不起作用
SQL Server 2016 max in partition by select statement not working
我正在尝试通过分区语句return 分组集的最小和最大日期时间。最小行有效,但最大行是 returning 未分组结果的最大日期。 SQL Server 2016。我错过了什么?
SELECT
[temp_Emp], [temp_EmpID], [temp_Date], [Temp_Start], [Temp_End],
MIN(Temp_Start) OVER (PARTITION BY temp_EmpID, temp_Date ORDER BY Temp_Start, Temp_End) AS 'Start',
MAX(Temp_End) OVER (PARTITION BY temp_EmpID, temp_Date ORDER BY Temp_Start, Temp_End) AS 'End'
FROM
tmp_startend
结果:
temp_EmpID temp_Date Temp_Start Temp_End Start End
300 7/08/2017 7/08/2017 8:00 7/08/2017 8:15 7/08/2017 8:00 7/08/2017 8:15
300 7/08/2017 7/08/2017 8:15 7/08/2017 8:30 7/08/2017 8:00 7/08/2017 8:30
300 7/08/2017 7/08/2017 9:00 7/08/2017 10:00 7/08/2017 8:00 7/08/2017 10:00
300 7/08/2017 7/08/2017 9:15 7/08/2017 14:30 7/08/2017 8:00 7/08/2017 14:30
300 7/08/2017 7/08/2017 9:30 7/08/2017 14:00 7/08/2017 8:00 7/08/2017 14:30
300 7/08/2017 7/08/2017 10:00 7/08/2017 13:00 7/08/2017 8:00 7/08/2017 14:30
300 7/08/2017 7/08/2017 10:00 7/08/2017 14:30 7/08/2017 8:00 7/08/2017 14:30
300 7/08/2017 7/08/2017 14:00 7/08/2017 15:00 7/08/2017 8:00 7/08/2017 15:00
省略 order by
。这使得值累积:
SELECT [temp_Emp], [temp_EmpID],[temp_Date],[Temp_Start],[Temp_End],
min(Temp_Start) over (partition by temp_EmpID, temp_Date) as [Start],
max(Temp_End) over (partition by temp_EmpID, temp_Date) as [End]
FROM tmp_startend;
min()
恰好起作用了,因为 order by
将最小值放在首位——因此累积 min()
与整体 min()
相同。
我正在尝试通过分区语句return 分组集的最小和最大日期时间。最小行有效,但最大行是 returning 未分组结果的最大日期。 SQL Server 2016。我错过了什么?
SELECT
[temp_Emp], [temp_EmpID], [temp_Date], [Temp_Start], [Temp_End],
MIN(Temp_Start) OVER (PARTITION BY temp_EmpID, temp_Date ORDER BY Temp_Start, Temp_End) AS 'Start',
MAX(Temp_End) OVER (PARTITION BY temp_EmpID, temp_Date ORDER BY Temp_Start, Temp_End) AS 'End'
FROM
tmp_startend
结果:
temp_EmpID temp_Date Temp_Start Temp_End Start End
300 7/08/2017 7/08/2017 8:00 7/08/2017 8:15 7/08/2017 8:00 7/08/2017 8:15
300 7/08/2017 7/08/2017 8:15 7/08/2017 8:30 7/08/2017 8:00 7/08/2017 8:30
300 7/08/2017 7/08/2017 9:00 7/08/2017 10:00 7/08/2017 8:00 7/08/2017 10:00
300 7/08/2017 7/08/2017 9:15 7/08/2017 14:30 7/08/2017 8:00 7/08/2017 14:30
300 7/08/2017 7/08/2017 9:30 7/08/2017 14:00 7/08/2017 8:00 7/08/2017 14:30
300 7/08/2017 7/08/2017 10:00 7/08/2017 13:00 7/08/2017 8:00 7/08/2017 14:30
300 7/08/2017 7/08/2017 10:00 7/08/2017 14:30 7/08/2017 8:00 7/08/2017 14:30
300 7/08/2017 7/08/2017 14:00 7/08/2017 15:00 7/08/2017 8:00 7/08/2017 15:00
省略 order by
。这使得值累积:
SELECT [temp_Emp], [temp_EmpID],[temp_Date],[Temp_Start],[Temp_End],
min(Temp_Start) over (partition by temp_EmpID, temp_Date) as [Start],
max(Temp_End) over (partition by temp_EmpID, temp_Date) as [End]
FROM tmp_startend;
min()
恰好起作用了,因为 order by
将最小值放在首位——因此累积 min()
与整体 min()
相同。