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 |
|
ranganath
Posting Yak Master
209 Posts |
Posted - 2007-07-03 : 08:59:52
|
| hi,Declare @Temp Table (Id int Identity(1,1),PubId int, StartDateTime Datetime)Insert into @Temp Select 1046,'2007-07-03 02:40:07.530' union all Select 1046, '2007-07-03 02:42:58.810'union allSelect 274 ,'2007-07-03 04:19:04.283'union allSelect 274, '2007-07-03 04:19:04.283'union allSelect 274, '2007-07-03 04:19:16.050'union allSelect 274 ,'2007-07-03 04:19:23.940'union allSelect 438 ,'2007-07-03 05:10:45.980'union allSelect 438 ,'2007-07-03 05:10:46.010'union allSelect 438, '2007-07-03 05:10:51.197'union allSelect 438, '2007-07-03 05:10:52.837'union allSelect 438, '2007-07-03 06:07:57.137'union allSelect 438, '2007-07-03 06:07:57.137'union allSelect 438, '2007-07-03 06:08:07.793'union allSelect 438, '2007-07-03 06:08:11.387'union allSelect 411, '2007-07-03 10:41:53.390'union allSelect 256, '2007-07-03 10:41:53.390'union allSelect 256, '2007-07-03 10:41:53.407'union allSelect 411, '2007-07-03 10:41:53.407'union allSelect 256, '2007-07-03 10:42:07.560'union allSelect 411, '2007-07-03 10:42:16.047'output:Select Pubid, count number of pubIds, timeinterval For Every one Minute My desired output as fallowspubId timeInterval count411 2007-07-03 10:41:53.390 - 2007-07-03 10:42:16.047 6256256411256411438 2007-07-03 06:07:57.137 - 2007-07-03 06:08:07.793 1 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-03 : 09:42:56
|
| You must provide more information about expected output.What are your business rules?Peter LarssonHelsingborg, Sweden |
 |
|
|
ranganath
Posting Yak Master
209 Posts |
Posted - 2007-07-03 : 10:35:31
|
| Hi,how many records (count of records) between '2007-07-03 10:41' and '2007-07-03 10:42' what are the pubids between '2007-07-03 10:41' and '2007-07-03 10:42' like that |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-03 : 11:31:13
|
| You mean your expected output is not complete, kind of sort of?Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-03 : 11:37:47
|
Your sample data do not meet your expected output critera.Try this-- prepare sample datadeclare @sample table (pubid int, startdatetime datetime)insert @sampleselect 1046,'2007-07-03 02:40:07.530' union all select 1046, '2007-07-03 02:42:58.810' union allselect 274 ,'2007-07-03 04:19:04.283' union allselect 274, '2007-07-03 04:19:04.283' union allselect 274, '2007-07-03 04:19:16.050' union allselect 274 ,'2007-07-03 04:19:23.940' union allselect 438 ,'2007-07-03 05:10:45.980' union allselect 438 ,'2007-07-03 05:10:46.010' union allselect 438, '2007-07-03 05:10:51.197' union allselect 438, '2007-07-03 05:10:52.837' union allselect 438, '2007-07-03 06:07:57.137' union allselect 438, '2007-07-03 06:07:57.137' union allselect 438, '2007-07-03 06:08:07.793' union allselect 438, '2007-07-03 06:08:11.387' union allselect 411, '2007-07-03 10:41:53.390' union allselect 256, '2007-07-03 10:41:53.390' union allselect 256, '2007-07-03 10:41:53.407' union allselect 411, '2007-07-03 10:41:53.407' union allselect 256, '2007-07-03 10:42:07.560' union allselect 411, '2007-07-03 10:42:16.047'-- show the expected outputselect d.pubid, convert(varchar, d.mindt, 120) + ' - ' + convert(varchar, d.maxdt, 120) as timeinterval, count(*) as [count]from ( select pubid, min(startdatetime) as mindt, max(startdatetime) as maxdt from @sample group by pubid ) as dinner join @sample as s on s.startdatetime between d.mindt and d.maxdtgroup by d.pubid, d.mindt, d.maxdtorder by d.pubid Peter LarssonHelsingborg, Sweden |
 |
|
|
ranganath
Posting Yak Master
209 Posts |
Posted - 2007-07-04 : 02:52:54
|
| hiIf timeInterval Gap is One Minute then show All Publicationid Between one minute like 2007-07-03 10:41:53 - 2007-07-03 10:42:16 have two pubid's they are 256,411 and count as 6If timeInterval Gap is more than one minute then leaveit,EX :2007-07-03 05:10:45 - 2007-07-03 06:08:11 then u have to show the pubids in the timeinterval 2007-07-03 05:10:45 and their count also seperatly and 2007-07-03 06:08:11 seperate records |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-04 : 03:36:48
|
| How do you calculate the "timeinterval gap"?You haven't provided enough details for us to solve this. What to do with PubID 438?Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-04 : 03:43:22
|
| [code]-- prepare sample datadeclare @sample table (pubid int, startdatetime datetime)insert @sampleselect 1046,'2007-07-03 02:40:07.530' union all select 1046, '2007-07-03 02:42:58.810' union allselect 274 ,'2007-07-03 04:19:04.283' union allselect 274, '2007-07-03 04:19:04.283' union allselect 274, '2007-07-03 04:19:16.050' union allselect 274 ,'2007-07-03 04:19:23.940' union allselect 438 ,'2007-07-03 05:10:45.980' union allselect 438 ,'2007-07-03 05:10:46.010' union allselect 438, '2007-07-03 05:10:51.197' union allselect 438, '2007-07-03 05:10:52.837' union allselect 438, '2007-07-03 06:07:57.137' union allselect 438, '2007-07-03 06:07:57.137' union allselect 438, '2007-07-03 06:08:07.793' union allselect 438, '2007-07-03 06:08:11.387' union allselect 411, '2007-07-03 10:41:53.390' union allselect 256, '2007-07-03 10:41:53.390' union allselect 256, '2007-07-03 10:41:53.407' union allselect 411, '2007-07-03 10:41:53.407' union allselect 256, '2007-07-03 10:42:07.560' union allselect 411, '2007-07-03 10:42:16.047'-- show the expected outputselect d.pubid, CASE WHEN DATEDIFF(ms, d.mindt, d.maxdt) >= 60000 THEN NULL ELSE convert(varchar, d.mindt, 120) + ' - ' + convert(varchar, d.maxdt, 120) END as timeinterval, count(*) as [count]from ( select pubid, min(startdatetime) as mindt, max(startdatetime) as maxdt from @sample group by pubid ) as dinner join @sample as s on s.startdatetime between d.mindt and d.maxdtgroup by d.pubid, d.mindt, d.maxdtorder by d.pubid[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|
|
|