When posting a question, you should really include some consumable test data that illustrates your problem.With the information provided something like:-- *** Test Data ***CREATE TABLE #t( Id int NOT NULL ,SCHEDULER_TEMPLATE_CODE varchar(20) NOT NULL ,[START_DATE] datetime NOT NULL ,END_DATE datetime NOT NULL ,[STATUS] char(1) NOT NULL ,[ERROR_MESSAGE] varchar(20) NOT NULL);INSERT INTO #t-- dates in ISO format!VALUES (1050711323, 'JOB_NAMEDA', '20140919 3:00:00', '20140919 3:00:12', 'F', 'Error description');-- *** End Test Data ***
This data does not really illustrate what you want to do.Try playing with something like:WITH JobOrderAS( SELECT * ,ROW_NUMBER() OVER (PARTITION BY DATEADD(day, DATEDIFF(day, 0, [START_DATE]), 0) ORDER BY [START_DATE] DESC) AS rn FROM #t WHERE SCHEDULER_TEMPLATE_CODE IN ('JOB_NAMEDA', 'JOB_NAMEDANOTHER', 'JOB_NAMELESS', 'JOB_ANOTHER_EXAMPLE'))SELECT DATEADD(day, DATEDIFF(day, 0, [START_DATE]), 0) AS StartDay ,Id AS JobID ,SCHEDULER_TEMPLATE_CODE AS JobName ,CAST([START_DATE] AS time) AS StartTime ,CAST(END_DATE AS time) AS EndTime ,DATEDIFF(s, [START_DATE], END_DATE) AS RunTime ,[STATUS] ,[ERROR_MESSAGE]FROM JobOrderWHERE rn = 1;