dev-exchange.com logo

dev-exchange.com topic RSS feed

Book mark: Add http://www.dev-exchange.com/viewtopic.php?t=76&start=0&postdays=0&postorder=asc&highlight=&sid=82dfdf44c99c87cf60668bb11fe80ec4 to your delicious account Add http://www.dev-exchange.com/viewtopic.php?t=76&start=0&postdays=0&postorder=asc&highlight=&sid=82dfdf44c99c87cf60668bb11fe80ec4 to your digg account Add http://www.dev-exchange.com/viewtopic.php?t=76&start=0&postdays=0&postorder=asc&highlight=&sid=82dfdf44c99c87cf60668bb11fe80ec4 to your blinklist account Add http://www.dev-exchange.com/viewtopic.php?t=76&start=0&postdays=0&postorder=asc&highlight=&sid=82dfdf44c99c87cf60668bb11fe80ec4 to your reddit account Add http://www.dev-exchange.com/viewtopic.php?t=76&start=0&postdays=0&postorder=asc&highlight=&sid=82dfdf44c99c87cf60668bb11fe80ec4 to Dzone Add http://www.dev-exchange.com/viewtopic.php?t=76&start=0&postdays=0&postorder=asc&highlight=&sid=82dfdf44c99c87cf60668bb11fe80ec4 to your furl account Add http://www.dev-exchange.com/viewtopic.php?t=76&start=0&postdays=0&postorder=asc&highlight=&sid=82dfdf44c99c87cf60668bb11fe80ec4 to your stumble account Add http://www.dev-exchange.com/viewtopic.php?t=76&start=0&postdays=0&postorder=asc&highlight=&sid=82dfdf44c99c87cf60668bb11fe80ec4 to your Yahoo myweb account
Stored Procedure to get average

 
Post new topic   Reply to topic   printer-friendly view    dev-exchange.com Forum Index -> Microsoft SQL SERVER
View previous topic :: View next topic  
Author Message
dotnetstarter
Contributing Member
Contributing Member


Joined: 19 Feb 2007
Posts: 177

PostPosted: Tue Feb 20, 2007 3:08 am    Post subject: Stored Procedure to get average Reply with quote

Hi All,

I am trying to write a stored procedure, where I need your help.

2 tables:
dbo.table1
- duration(time) eg: 00:01:34
-calldate
-caseid

dbo.table2
-ID
-caseid

I want to get the average of duration, between given dates supplied(eg @startDate, @endDate -- calldate) , and ID
One more issue is, I have the ID in table2 as the parameter.
In table2, there is no ID specified, only caseid is there.

How can I get this done.

Hope someone can help me

Thanks
Back to top
View user's profile Send private message
Kishore
Contributing Member
Contributing Member


Joined: 20 Feb 2007
Posts: 202
Location: Banglore , India

PostPosted: Tue Feb 20, 2007 10:58 am    Post subject: Reply with quote

You can use AVG function to get the average of duration but you need to convert the duration into seconds so that it will give you average. You need to play around with following example.

Code:
CREATE PROCEDURE usp_getavgduration(@STARTDT DATETIME,@ENDDT DATETIME,@ID INT) AS

BEGIN

SELECT AVG(CAST(CAST(CONVERT(char(8), t1.duration, 108) AS datetime) AS numeric(18, 4)) * 24) AS AverageDuration
  FROM table1 t1
 INNER JOIN table2 t2 ON t1.caseid =t2.caseid
 WHERE t2.ID=@ID
   AND (t1.calldate between @STARTDT and @ENDDT)

END
GO


Hope this will help
_________________
=================
Kish
Back to top
View user's profile Send private message
dotnetstarter
Contributing Member
Contributing Member


Joined: 19 Feb 2007
Posts: 177

PostPosted: Tue Feb 20, 2007 1:56 pm    Post subject: Gives null Reply with quote

Thanks for the reply

I tried with your peice of code.
But I am getting a null value for the inputs
I checked the data manually, and there is call duration between the dates which i gave

Can you please suggest some other modification??
Back to top
View user's profile Send private message
Kishore
Contributing Member
Contributing Member


Joined: 20 Feb 2007
Posts: 202
Location: Banglore , India

PostPosted: Tue Feb 20, 2007 3:18 pm    Post subject: Reply with quote

please dump few duration records here.
_________________
=================
Kish
Back to top
View user's profile Send private message
Kishore
Contributing Member
Contributing Member


Joined: 20 Feb 2007
Posts: 202
Location: Banglore , India

PostPosted: Tue Feb 20, 2007 3:23 pm    Post subject: Reply with quote

what is the duration field ? is it a datetime field?
_________________
=================
Kish
Back to top
View user's profile Send private message
dotnetstarter
Contributing Member
Contributing Member


Joined: 19 Feb 2007
Posts: 177

PostPosted: Tue Feb 20, 2007 3:23 pm    Post subject: Sample data Reply with quote

00:04:53
00:06:23
00:20:02

These are some sample data for duration

My calldate data also contain time part
as
2005-12-16 14:10:23

Duration field is nvarchar
CallDate field is smalldatetime


Last edited by dotnetstarter on Tue Feb 20, 2007 3:32 pm; edited 1 time in total
Back to top
View user's profile Send private message
Kishore
Contributing Member
Contributing Member


Joined: 20 Feb 2007
Posts: 202
Location: Banglore , India

PostPosted: Tue Feb 20, 2007 3:31 pm    Post subject: Reply with quote

Can you try calldate instead of duration in the query for a test?
_________________
=================
Kish
Back to top
View user's profile Send private message
dotnetstarter
Contributing Member
Contributing Member


Joined: 19 Feb 2007
Posts: 177

PostPosted: Tue Feb 20, 2007 3:34 pm    Post subject: Reply with quote

Gives
.000000
Back to top
View user's profile Send private message
Kishore
Contributing Member
Contributing Member


Joined: 20 Feb 2007
Posts: 202
Location: Banglore , India

PostPosted: Tue Feb 20, 2007 3:36 pm    Post subject: Reply with quote

what data type is duration field?
_________________
=================
Kish
Back to top
View user's profile Send private message
dotnetstarter
Contributing Member
Contributing Member


Joined: 19 Feb 2007
Posts: 177

PostPosted: Tue Feb 20, 2007 3:40 pm    Post subject: Reply with quote

Duration field is nvarchar
CallDate field is smalldatetime
Back to top
View user's profile Send private message
Kishore
Contributing Member
Contributing Member


Joined: 20 Feb 2007
Posts: 202
Location: Banglore , India

PostPosted: Tue Feb 20, 2007 3:42 pm    Post subject: Reply with quote

ok let me try something else. i'll test and tell you
_________________
=================
Kish
Back to top
View user's profile Send private message
dotnetstarter
Contributing Member
Contributing Member


Joined: 19 Feb 2007
Posts: 177

PostPosted: Tue Feb 20, 2007 3:47 pm    Post subject: New Reply with quote

I tried with another select stmt, like

SELECT 24 * AVG(DATEDIFF(second, 0, t1.duration)) AS AverageDuration
FROM Table1 AS t1
INNER JOIN Table2 AS t2 ON t2.CaseID = t1.CaseID
WHERE t2.ID = @ID
AND t1.CallDate >= @StartDT
AND t1.CallDate < DATEADD(day, 1, @EndDT)

but it gives the average as 6246, 5623 etc

i put exec getavgduration_test '5/13/2005', '5/16/2005', 55
the data for the id 55, during this time period is

00:01:48
00:12:50
00:04:03
00:01:32
00:01:44
00:04:49
00:02:46

the result i am getting is 6192!!!!

Can you please go trough this select statement too

Thanks
Back to top
View user's profile Send private message
dotnetstarter
Contributing Member
Contributing Member


Joined: 19 Feb 2007
Posts: 177

PostPosted: Tue Feb 20, 2007 3:58 pm    Post subject: Got it worked Reply with quote

Thanks

I got it worked with a new select stamtement
Just wanted to share

SELECT CONVERT(varchar, DATEADD(second, AVG(DATEDIFF(second, 0, t1.duration)), 0), 108) AS AverageDuration
FROM Table1 AS t1
INNER JOIN Table2 AS t2 ON t2.CaseID = t1.CaseID
WHERE t2.ID = @ID
AND t1.CallDate >= @StartDT
AND t1.CallDate < DATEADD(day, 1, @EndDT)
Back to top
View user's profile Send private message
Kishore
Contributing Member
Contributing Member


Joined: 20 Feb 2007
Posts: 202
Location: Banglore , India

PostPosted: Tue Feb 20, 2007 4:18 pm    Post subject: Reply with quote

Ok great! I was something similar but didnt work on my SQL server. I use SQL Server 2000.

Any way nice to see that you have sorted the isssue.

Goodluck!
_________________
=================
Kish
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    dev-exchange.com Forum Index -> Microsoft SQL SERVER All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


© 2008 dev-exchange.com
Powered by phpBB. Theme DEVPPL.