我想问一下当有大量重复数据时如何获得最大日期和最长时间。
我正在使用此查询来显示数据
/ ******来自SSMS ****** /
的SelectTopNRows命令的脚本SELECT
trx.[ActivityDate]
,trx.ActivityTimeStart
,trx.ActivityTimeEnd
,trx.[EmployeeID]
,trx.[ApprovalStatusID]
,mst.[FullName]
FROM [KairosManagementSystem].[dbo].[Tbl_Trx_TimeSheet] trx
INNER JOIN [dbo].[Tbl_Mst_Employee] mst
ON trx.[EmployeeID] = mst.[EmployeeID]
where datepart(year,trx.[ActivityDate]) between datepart(year, dateadd(year,-1,getdate())) and datepart(year, getdate())
and
trx.[ActivityDate] <= getdate()
and
trx.EmployeeID = 11460
order by trx.[ActivityDate] DESC
问题是如何得到这样的结果。我希望在相应的日期
中获得最小的ActivitityTimeStart和最大的ActivityTimeEnd ----------------------------------------------------------------------------
|ActivityDate | ActivityTimeStart | ActivityTimeEnd | EmployeeID | FullName
----------------------------------------------------------------------------
|2017-02-17 | 07:00:00 00:00:00 | 16:00:00 00:00:00| 11460 | Yohanes
答案 0 :(得分:1)
您只想要一个简单的GROUP BY
吗?
SELECT trx.[ActivityDate], MIN(trx.ActivityTimeStart), MAX(trx.ActivityTimeEnd),
trx.[EmployeeID], mst.[FullName]
FROM [KairosManagementSystem].[dbo].[Tbl_Trx_TimeSheet] trx INNER JOIN
[dbo].[Tbl_Mst_Employee] mst
ON trx.[EmployeeID] = mst.[EmployeeID]
WHERE YEAR(trx.[ActivityDate]) between YEAR(dateadd(year,-1,getdate())) and YEAR(getdate()) and
trx.[ActivityDate] <= getdate() and
trx.EmployeeID = 11460
GROUP BY trx.[ActivityDate], trx.[EmployeeID], mst.[FullName]
ORDER BY trx.[ActivityDate] DESC;