15.7.09

Difference between the dates group by id

Scenario:

I got a nice question in a forum on getting the difference between the created date and the updated date for a particular id. The complication is, we will have lot of records with that id. We need to group it.


Solution:

The solution is too simple,


drop table venkat
create table venkat(id int, date1 datetime,date2 datetime)
insert into venkat values(1,'1/2/2009','3/3/2009')
insert into venkat values(1,'2/2/2009','4/3/2009')
insert into venkat values(2,'2/2/2009','3/3/2009')
insert into venkat values(2,'2/2/2009','4/3/2009')
select * from venkat
select id,datediff(dd,min(date1),max(date2)) as datedifferenc from venkat group by id

Thanks and Regards,

Venkatesan Prabu .J

No comments:

Post a Comment