Avatar of Dave KIlby
Dave KIlby
Flag for Canada asked on

MS SQL Last Entry For Day

I would like to pull a report that shows me the last entry by an employee on a given day.

The fields are Date, Time, Employee, Status, Reason  - I would like to see the last entry for each employee for a given date which will be in the where statement

Thanks
Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
Ryan Chong

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
smilieface

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Ryan Chong

another method is using Row_Number clause, such as

;with yourTable as
(
	Select '13 Jan 2020 13:13pm' yourDateTime, 'A' Employee, '1' Status, 'Bla bla..1' Reason union all
	Select '13 Jan 2020 13:14pm' yourDateTime, 'A' Employee, '1' Status, 'Bla bla..2' Reason union all
	Select '13 Jan 2020 13:15pm' yourDateTime, 'A' Employee, '1' Status, 'Bla bla..3' Reason union all
	Select '14 Jan 2020 13:13pm' yourDateTime, 'A' Employee, '1' Status, 'Bla bla..4' Reason union all
	Select '15 Jan 2020 13:13pm' yourDateTime, 'A' Employee, '1' Status, 'Bla bla..5' Reason union all
	Select '13 Jan 2020 13:13pm' yourDateTime, 'B' Employee, '1' Status, 'Bla bla..6' Reason union all
	Select '13 Jan 2020 13:13pm' yourDateTime, 'C' Employee, '1' Status, 'Bla bla..7' Reason union all
	Select '14 Jan 2020 13:13pm' yourDateTime, 'C' Employee, '1' Status, 'Bla bla..8' Reason union all
	Select '15 Jan 2020 13:13pm' yourDateTime, 'D' Employee, '1' Status, 'Bla bla..9' Reason
), cte as
(
	Select *, ROW_NUMBER() over (partition by Employee order by yourDateTime desc) idx
	from yourTable
)
Select * from cte
Where idx = 1
order by Employee

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes