2017-03-06 10 views
0

重複データが多い場合に最大日時と最大時間を取得する方法について質問したいと思います。重複テーブルの最大日時を取得するSQL Server

iは、データ

を表示するには、このクエリを使用しています

/******スクリプトSelectTopNRowsはSSMSからのコマンドを******/

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 

あなたは、見ることができるようにこの結果は次のようになります。 enter image description here

このような結果を得る方法はどうですか?私は、それぞれの日付に

---------------------------------------------------------------------------- 
|ActivityDate | ActivityTimeStart | ActivityTimeEnd | EmployeeID | FullName 
---------------------------------------------------------------------------- 
|2017-02-17 | 07:00:00 00:00:00 | 16:00:00 00:00:00| 11460  | Yohanes 

答えて

1

を最小ActivitityTimeStartと最大ActivityTimeEndを取得したい場所あなただけのシンプルな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; 
+0

非常にありがとうございました。魅力的です –

関連する問題