Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
SQL
Select closing_date,employee_id from shift_details group by MAX(sd_id)


What I have tried:

SQL
SELECT MAX(sd_id) AS sd_id FROM shift_details
Posted
Updated 24-Oct-22 1:30am
v2
Comments
Richard Deeming 20-Oct-22 10:23am    
You've forgotten to ask a question. We have no idea which part you're stuck with: writing the SQL query to get the max ID; executing that query from C#; triggering the C# code from some sort of WPF event; or something else entirely.

1 solution

Based on the SQL you have posted it looks as if you are trying to get closing_date and employee_id from shift details for the row containing the maximum sd_id.

There are many ways you can achieve this

1. Declare a variable, assign the max sd_id value to it and select from the table for that id e.g.
SQL
declare @maxid int = (SELECT MAX(sd_id) AS sd_id FROM shift_details);
Select closing_date,employee_id from shift_details 
where sd_id = @maxid;
2. Use a sub-query to get that value and filter the results on that sub-query e.g.
SQL
Select closing_date,employee_id from shift_details 
where sd_id = (SELECT MAX(sd_id) from shift_details);
3. Do an inner join on a sub-query e.g.
SQL
Select closing_date,employee_id from shift_details a
inner join (SELECT MAX(sd_id) as maxid from shift_details) b on a.sd_id = b.maxid;
4. Use a combination of ORDER BY and TOP (N.B. This is not a recommended approach, look at the execution plans if you want to see why. I'm only including it here for illustrative purposes.) e.g.
SQL
Select top 1 closing_date,employee_id from shift_details order by sd_id desc;
If this is not what you were trying to do then update your question with more information, and reply to this post and I will try to help you
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900