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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Finding Simultaneous entries

Author  Topic 

drewgy
Starting Member

4 Posts

Posted - 2009-01-21 : 10:18:10
Hi,

I have a table that logs the sending of faxes with the following structure:

ID DATESTAMP DURATION DESCRIPTION CHANNEL
1 2008-11-28 12:42:25.000 27 Document delivered. 7
2 2008-11-28 12:46:12.000 14 Document delivered. 7
3 2008-11-28 12:47:52.000 15 Document delivered. 7
4 2008-11-28 12:49:32.000 14 Document delivered. 7
5 2008-11-28 12:49:33.000 15 Document delivered. 8
6 2008-11-28 12:53:13.000 15 Document delivered. 6

I need to extract information about the maximum number of simultaneous channels in use for a 24 hour period and n days period (ie 2 are simultaneously in use in the example above).

I have to admit that I dont have a clue about how to do this, so any help would be much appreciated.

Cheers

Drewgy

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-01-21 : 10:34:37
what unit of time is [duration] - seconds? Are the 2 simultaneous channels rows 4 and 5 because they are the only two rows where different channels are in use at the same time? And just confirming that you are using SS 2005?

Be One with the Optimizer
TG
Go to Top of Page

drewgy
Starting Member

4 Posts

Posted - 2009-01-21 : 10:40:08
TG,

Yes duration is in seconds and rows 4 & 5 are the simultaneous ones.

And I am using SS 2005.

Cheers

Drewgy
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-01-21 : 10:47:49
Is the 24 hour period a specific period (ie: 00:00.000 - 24:59.999) or any rolling 24 hours?
And is that for any specific n days or any rolling n days?

Be One with the Optimizer
TG
Go to Top of Page

drewgy
Starting Member

4 Posts

Posted - 2009-01-21 : 10:55:59
TG,

The 24 hour period is specific ie. 2008-11-28 00:00:00 to 2008-11-28 23:59:59.

The n days period is rolling ie. 14 days starting 2008-11-28, 8 days starting 2008-10-01 etc.

Drewgy
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-01-21 : 10:59:39
See if this works:

set nocount on
declare @yourTable table (ID int, DATESTAMP datetime, DURATION int, DESCRIPTION varchar(30), CHANNEL int)
insert @yourTable
select 1, '2008-11-28 12:42:25.000', 27, 'Document delivered.', 7 union all
select 2, '2008-11-28 12:46:12.000', 14, 'Document delivered.', 7 union all
select 3, '2008-11-28 12:47:52.000', 15, 'Document delivered.', 7 union all
select 4, '2008-11-28 12:49:32.000', 14, 'Document delivered.', 7 union all
select 5, '2008-11-28 12:49:33.000', 15, 'Document delivered.', 8 union all
select 6, '2008-11-28 12:53:13.000', 15, 'Document delivered.', 6


declare @start datetime
,@numDays int
select @start = '2008-11-28'
,@numDays = 14

;with cte
as
(
select id
,datestamp st
,dateadd(second, duration, datestamp) ed
,channel
from @yourTable
)
select dateadd(day, datediff(day, 0, a.st), 0) dy
,dateadd(hour, datediff(hour, 0, a.st), 0) hr
,count(*) * 2 [simul_count]
from cte a
inner join cte b
on b.st >= a.st
and b.st <= a.ed
and b.channel != a.channel
where a.st >= @start
and a.st <= dateadd(day, @numDays, @start)
group by dateadd(day, datediff(day, 0, a.st), 0)
,dateadd(hour, datediff(hour, 0, a.st), 0)


output:
dy hr simul_count
----------------------- ----------------------- -----------
2008-11-28 00:00:00.000 2008-11-28 12:00:00.000 2


EDIT:
modified based on OP request (below)

Be One with the Optimizer
TG
Go to Top of Page

drewgy
Starting Member

4 Posts

Posted - 2009-01-21 : 11:25:46
TG,

Thanks for your reply - though its not quite what I was after.

I need a result for each hour or day in the period as I need to use the data to produce a graph that displays maximum channel usage for the period.

ie. 24 hours split into 00:00 - 01:00, 01:00 - 02:00, 02:00 - 03:00 etc
and n days split into 28/11, 29/11, 30/11, 01/12 etc.

Cheers

Drewgy
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-01-21 : 11:35:38
You're welcome.
That should just be splitting the [period] into two groupings.
I'll modify the statement above.

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -