I'm not sure how you calculated 551 days or which date column 06/21/05 belongs to and what to do if the other column is null. But maybe this will give you some ideas:set nocount on--DDLdeclare @tb table (Empid int, jobcode varchar(5), jobStartDate datetime, JobEndDate datetime, JobReason varchar(20))--DMLinsert @tbselect 123, 'GKJ79', '01/01/2004', '12/31/2004', 'Promo' union allselect 123, 'RFTYD', '01/01/2005', '04/01/2005', 'JobRename' union allselect 123, 'GKJ79', '04/02/2005', '06/20/2005', 'Promo' union allselect 123, 'GKJ79', '06/21/2005', null, 'Realignment'--select statementselect empid ,jobcode ,sum(datediff(day, jobStartDate, isnull(JobEndDate,getdate()))) totalhoursfrom @tb--where jobReason <> 'JobRename'--and jobcode = 'GKJ79'--and jobstartDate <> '1/1/2005'group by empid ,jobcode
Be One with the OptimizerTG