How to Find Single Row Records from Two Rows Data with AttTime in SQL Server
I have query to find Records in to single records from two rows with First AttTime and Last AttTime using SQL Server 2005
My AttLog Table Structure is:
EnrollNo Int
AttDate DateTime
AttMonth Int
AttDay Int
AttYear Int
AttTime Varchar
Current output:
EnrollNo AttDate AttMonth AttDay AttYear AttTime
405 2011-03-09 09:59:00.000 9 3 2011 9:59
405 2011-03-09 18:40:00.000 9 3开发者_如何学运维 2011 18:40
Desired output:
EnrollNo AttDate AttMonth AttDay AttYear FirstTime Last Time
405 2011-03-09 09:59:00.000 9 3 2011 9:59 18:40
Assuming you are grouping records with same EnrollNo, you should use aggregate functions
SELECT EnrollNo, Min(AttDate), Min(AttMonth), Min(AttDay), Min(AttYear), Min(AttTime) as FirstTime, Max(AttTime) as LastTime
FROM AttLog
GROUP BY EnrollNo
精彩评论