Hi Friends,
I have the following table structure
ID Datetime status
1 2025-10-15 1:03:53 PM T2
1 2025-10-15 12:37:42 PM T2
1 2025-10-15 12:41:22 PM T2
3 2025-10-15 12:26:18 PM T2
3 2025-10-15 12:21:47 PM T2
I need the output in the sql server
Status ID startdate Enddate
T2 1 2025-10-15 12:41:22 PM 2025-10-15 1:03:53 PM
T2 3 2025-10-15 12:21:47 PM 2025-10-15 12:26:18 PM
Prasad RaveendranPosted Oct 17, 2025, 1:54 AM
try this:
SELECT
status,
ID,
MIN([Datetime]) AS startdate,
MAX([Datetime]) AS enddate
FROM
YourTable
GROUP BY
status,
ID
ORDER BY
ID, startdate;
Andrew WestonPosted Oct 15, 2025, 5:52 PM
You can easily find the start and end datetime in SQL Server using the
MIN()andMAX()functions. For example:SELECT MIN(DateColumn) AS StartDateTime, MAX(DateColumn) AS EndDateTime FROM YourTableName;This query returns the earliest and latest datetime from your data. Super handy for reports or log tracking! Also, if you create SQL tutorials or videos, you can design custom visuals using the YouTube Thumbnail Downloader to make your content stand out.