views:

83

answers:

4

Hi guys,

I have this thing that i need to do and some advices will be greatly appreciated. I have a SQL server table with some phone calls.For each phone call i have the start and end time.

What i need to accomplish: a stored procedure which for a certain period of time, let's say 5 hours at a x interval, lets say 2 minutes returns the number of connected calls.

Something like:

Interval                                    Nr of Calls Connected
01-01-2010 12:00:00 - 01-01-2010 12:05:00   30
01-01-2010 12:05:01 - 01-01-2010 12:10:00   10
.............

Which will be the fastest way to do that? Thank you for your help

A: 

How about this approach:

select Year(StartTime) as Year, Month(StartTime) as Month, Day(StartTime) as Day, datepart(hh, StartTime) as Hour, datepart(mm, StartTime) / 2 as TwoMinuteSegment, count(*)
from MyTable
where StartDate between '01-01-2010 12:00:00' and '01-01-2010 17:00:00'
group by Year(StartTime), Month(StartTime), Day(StartTime), datepart(hh, StartTime), datepart(mm, StartTime) / 2
RedFilter
+1  A: 

I Altered Gaby's example a little to do What you expected

Declare @datetimeend datetime
,@datetimecurrent datetime
,@interval int
Set @interval = 10
Set @datetimeend = (Select max([end_time]) from Calls)
SET @datetimecurrent = '2010-04-17 14:20:00'

declare @temp as table ([start_interval] datetime, [end_interval] datetime)

while  @datetimecurrent < @datetimeend
BEGIN
  insert into @temp select (@datetimecurrent), dateAdd( minute, @interval, @datetimecurrent)
  set @datetimecurrent = dateAdd( minute, @interval, @datetimecurrent)
END

Select 
  [start_interval],[end_interval], count(d.id) [COUNT]
From @temp t 
left join Calls d on 
    d.end_time >= t.start_interval
AND d.start_time <= t.end_interval
GROUP BY [start_interval],[end_interval]

used this to create the table and fill it

CREATE TABLE dbo.Calls
    (
    id int NOT NULL IDENTITY (1, 1),
    start_time datetime NOT NULL,
    end_time datetime NULL,
    caller nvarchar(50) NULL,
    receiver nvarchar(50) NULL
    )  ON [PRIMARY]
GO
ALTER TABLE dbo.Calls ADD CONSTRAINT
    PK_Calls PRIMARY KEY CLUSTERED 
    (
    id
    )  ON [PRIMARY]

GO

DECLARE @I INT
SET @I = 0
WHILE @I < 100
BEGIN
INSERT INTO Calls
(start_time, end_time)
select 
DATEADD(HOUR,-@I,DATEADD(MINUTE,-10,GETDATE()))
,DATEADD(HOUR,-@I,DATEADD(MINUTE,-9,GETDATE()))
UNION
select 
DATEADD(HOUR,-@I,DATEADD(MINUTE,-9,GETDATE()))
,DATEADD(HOUR,-@I,DATEADD(MINUTE,-8,GETDATE()))
UNION
select 
DATEADD(HOUR,-@I,DATEADD(MINUTE,-8,GETDATE()))
,DATEADD(HOUR,-@I,DATEADD(MINUTE,-7,GETDATE()))
UNION
select 
DATEADD(HOUR,-@I,DATEADD(MINUTE,-7,GETDATE()))
,DATEADD(HOUR,-@I,DATEADD(MINUTE,-6,GETDATE()))
UNION
select 
DATEADD(HOUR,-@I,DATEADD(MINUTE,-6,GETDATE()))
,DATEADD(HOUR,-@I,DATEADD(MINUTE,-5,GETDATE()))
UNION
SELECT
DATEADD(HOUR,-@I,DATEADD(MINUTE,-5,GETDATE()))
,DATEADD(HOUR,-@I,DATEADD(MINUTE,-4,GETDATE()))
UNION
select 
DATEADD(HOUR,-@I,DATEADD(MINUTE,-4,GETDATE()))
,DATEADD(HOUR,-@I,DATEADD(MINUTE,-3,GETDATE()))
UNION
select 
DATEADD(HOUR,-@I,DATEADD(MINUTE,-3,GETDATE()))
,DATEADD(HOUR,-@I,DATEADD(MINUTE,-2,GETDATE()))
UNION
select 
DATEADD(HOUR,-@I,DATEADD(MINUTE,-2,GETDATE()))
,DATEADD(HOUR,-@I,DATEADD(MINUTE,-1,GETDATE()))
UNION
select 
DATEADD(HOUR,-@I,DATEADD(MINUTE,-1,GETDATE()))
,DATEADD(HOUR,-@I,DATEADD(MINUTE,-0,GETDATE()));
SET @I = @I + 1
END    

Done in SQL Server 2008 but the script would work in other versions

Gabriel Guimarães
+2  A: 

This will work for intervals that have calls ...

Declare @datetimestart datetime
Declare @interval int
Set @datetimestart = '2009-01-01 12:00:00'
Set @interval = 5 --in minutes

Select
  [start_interval], [end_interval] , count([start_interval]) as [calls]
From
  (
    Select 
     DateAdd( Minute,Floor(DateDiff(Minute,@datetimestart,[date])/@interval)*@interval
,@datetimestart) , 
    DateAdd( Minute,@interval + Floor(DateDiff(Minute,@datetimestart,[date])/@interval)*@interval
,@datetimestart) 
    From yourTable
  ) As W([start_interval],[end_interval])
group by [start_interval], [end_interval]

This will work for all intervals regardless of number of calls..

Declare @datetimestart datetime, @datetimeend datetime, @datetimecurrent datetime
Declare @interval int
Set @datetimestart = '2009-01-01 12:00:00'
Set @interval = 10
Set @datetimeend = (Select max([date]) from yourtable)

SET @datetimecurrent = @datetimestart

declare @temp as table ([start_interval] datetime, [end_interval] datetime)

while  @datetimecurrent < @datetimeend
BEGIN
  insert into @temp select (@datetimecurrent), dateAdd( minute, @interval, @datetimecurrent)
  set @datetimecurrent = dateAdd( minute, @interval, @datetimecurrent)
END

Select
  *
From
  (
    Select 
      [start_interval],[end_interval], count(d.[start_time]) 
    From @temp t left join yourtable d on d.[start_time] between t.[start_interval] and t.[end_interval]
  ) As W([start_interval],[end_interval], [calls])
Gaby
Hi Gaby,Thank you for your help. I was looking over the last query to include all intervals and this From @temp t left join yourtable d on d.[start_time] between t.[start_interval] and t.[end_interval]Is taking in consideration only calls started in that interval and is omitting calls with startdate< interval start and enddate>interval end. Those calls are still conected and they should be included
stefan
@stefan, i see ... so a call could get counted in more than one intervals, right ?
Gaby
@stefan, if so the following should cover all cases (*i think*) `on ( (d.[start_time] between t.[start_interval] and t.[end_interval] ) OR (d.[end_time] between t.[start_interval] and t.[end_interval] ) OR (d.[start_time] < t.[start_interval] AND d.[end_time] > t.[end_interval])`
Gaby
just this will do. d.end_time >= t.start_intervalAND d.start_time <= t.end_interval
Gabriel Guimarães
@Gabriel lol ... of'course you are right .. i over complicated it .. *facepalm* ..
Gaby
Thank you Gaby and Gabriel. Excellent solution
stefan
+1  A: 

I would use a Numbers pivot table to get the time intervals and then count all calls which overlapped the interval:

SELECT Intervals.IntervalStart
    ,Intervals.IntervalEnd
    ,COUNT(*)
FROM (
    SELECT DATEADD(MINUTE, Numbers * 2, @StartTime) AS IntervalStart
        ,DATEADD(MINUTE, (Numbers + 1) * 2, @StartTime) AS IntervalEnd
    FROM Numbers
    WHERE Numbers BETWEEN 0 AND (5 * 60 / 2)
) AS Intervals
LEFT JOIN Calls
    ON Calls.CallEnd >= Intervals.IntervalStart
        AND Calls.CallStart < Intervals.IntervalEnd
GROUP BY Intervals.IntervalStart
    ,Intervals.IntervalEnd

To get the empty intervals, you would need to LEFT JOIN to this from another "Intervals" derived table.

Cade Roux