What is the datatype of Log_Date? This code assumes either varchar or the time components are all "00:00:00.000". Post back if that is not the case.set nocount on--generate original tableselect '14/feb/06' log_date, 'CLOSED' status into #origTable union allselect '14/feb/06', 'OPEN' union allselect '14/feb/06', 'WAITING' union allselect '16/feb/06', 'WAITING' union allselect '16/feb/06', 'WAITING' union allselect '16/feb/06', 'CLOSED' union allselect '16/feb/06', 'WAITING' union allselect '16/feb/06', 'WAITING' union allselect '16/feb/06', 'UNSOLVED' union allselect '16/feb/06', 'UNSOLVED' union allselect '17/feb/06', 'CLOSED' union allselect '19/feb/06', 'WAITING' union allselect '19/feb/06', 'WAITING' union allselect '19/feb/06', 'CLOSED' union allselect '19/feb/06', 'WAITING' union allselect '19/feb/06', 'UNSOLVED' union allselect '20/feb/06', 'UNSOLVED' union allselect '20/feb/06', 'UNSOLVED' union allselect '20/feb/06', 'WAITING' union allselect '21/feb/06', 'CLOSED' union allselect '21/feb/06', 'OPEN' union allselect '21/feb/06', 'OPEN'create table #dest (log_date varchar(10), [open] int, closed int, unsolved int, waiting int)insert #dest (log_date, [open], closed, unsolved, waiting)select [log_date] ,[open] = sum(case when status = 'open' then 1 else 0 end) ,closed = sum(case when status = 'closed' then 1 else 0 end) ,unsolved = sum(case when status = 'unsolved' then 1 else 0 end) ,waiting = sum(case when status = 'waiting' then 1 else 0 end)from #origTablegroup by [log_date]select * from #destdrop table #origTabledrop table #destoutput:log_date open closed unsolved waiting --------- ----------- ----------- ----------- ----------- 14/feb/06 1 1 0 116/feb/06 0 1 2 417/feb/06 0 1 0 019/feb/06 0 1 1 320/feb/06 0 0 2 121/feb/06 2 1 0 0
EDIT:I added some sample data to make sure it worked.
Be One with the OptimizerTG