Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Sachin.Nand
2937 Posts |
Posted - 2009-11-06 : 05:49:07
|
Hi guys,declare @tbl as table(typ int,stdt datetime,enddt datetime,diff int)insert into @tblselect 1,'2009-08-12 08:01:00.000', '2009-08-12 08:04:00.000',3 union allselect 1,'2009-08-12 08:01:00.000', '2009-08-12 08:08:00.000',7 union allselect 1,'2009-08-12 08:04:00.000' ,'2009-08-12 08:08:00.000',4 union allselect 1,'2009-08-12 08:08:00.000', '2009-08-12 08:18:00.000',10select * from @tbl i want that the resultset to be like this 1) 2009-08-12 08:01:00.000 and 2009-08-12 08:08:00.0002) 2009-08-12 08:08:00.000 and 2009-08-12 08:18:00.000because the difference between 2009-08-12 08:01:00.000 and 2009-08-12 08:08:00.000 is less than 10and the difference between2009-08-12 08:08:00.000 and 2009-08-12 08:18:00.000 is 10PBUH |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2009-11-06 : 07:56:18
|
| This?Select min(stdt),max(enddt)from @tblwhere diff < 10union allSelect min(stdt),max(enddt)from @tblwhere diff >= 10JimEveryday I learn something that somebody else already knew |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2009-11-06 : 08:23:18
|
quote: Originally posted by jimf This?Select min(stdt),max(enddt)from @tblwhere diff < 10union allSelect min(stdt),max(enddt)from @tblwhere diff >= 10JimEveryday I learn something that somebody else already knew
The value 10 is configurable & can be anything.Now if we tk the below sample data declare @tbl as table(typ int,stdt datetime,enddt datetime,diff int)insert into @tblselect 1,'2009-08-12 08:01:00.000', '2009-08-12 08:04:00.000',3 union allselect 1,'2009-08-12 08:01:00.000', '2009-08-12 08:08:00.000',7 union allselect 1,'2009-08-13 13:06:00.000' ,'2009-08-13 13:08:00.000',2 union allselect 1,'2009-08-12 10:56:00.000', '2009-08-12 11:03:00.000',7select * from @tblSelect min(stdt),max(enddt)from @tblwhere (diff) < 10union allSelect min(stdt),max(enddt)from @tblwhere diff >= 10I should get output as 2009-08-12 08:01:00.000,2009-08-12 08:08:00.0002009-08-13 13:06:00.000,2009-08-13 13:08:00.0002009-08-12 10:56:00.000,2009-08-12 11:03:00.000because the diff between 2009-08-12 08:01:00.000 & 2009-08-12 08:08:00.000 is less that 10. But by your query I get output 2009-08-12 08:01:00.000,2009-08-13 13:08:00.000PBUH |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2009-11-06 : 08:32:33
|
| Select min(stdt),max(enddt)from @tblwhere diff < 10group by stdtUNION ALL Select min(stdt),max(enddt)from @tblwhere diff >= 10group by stdtJimEveryday I learn something that somebody else already knew |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2009-11-06 : 08:48:53
|
| No still not working.declare @tbl as table(stdt datetime,enddt datetime,diff int)insert into @tblselect '2009-08-12 08:08:00.000', '2009-08-12 08:18:00.000' ,10 union allselect'2009-08-12 08:01:00.000', '2009-08-12 08:08:00.000', 7 union allselect '2009-08-12 08:01:00.000', '2009-08-12 08:18:00.000' ,17 union allselect '2009-08-12 08:01:00.000', '2009-08-12 08:04:00.000' ,3 union allselect '2009-08-12 08:04:00.000', '2009-08-12 08:08:00.000', 4 union allselect '2009-08-12 08:04:00.000', '2009-08-12 08:18:00.000',14select * from @tblSelect min(stdt),max(enddt)from @tblwhere diff < 10group by stdtUNION ALL Select min(stdt),max(enddt)from @tblwhere diff >= 10group by stdtPBUH |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2009-11-06 : 08:51:01
|
| I know the data is stupid but thats the way it is.I need to find a way out :(PBUH |
 |
|
|
|
|
|
|
|