| View previous topic :: View next topic |
| Author |
Message |
dotnetstarter Contributing Member

Joined: 19 Feb 2007 Posts: 177
|
Posted: Tue Feb 20, 2007 3:08 am Post subject: Stored Procedure to get average |
|
|
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 |
|
 |
Kishore Contributing Member

Joined: 20 Feb 2007 Posts: 202 Location: Banglore , India
|
Posted: Tue Feb 20, 2007 10:58 am Post subject: |
|
|
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 |
|
 |
dotnetstarter Contributing Member

Joined: 19 Feb 2007 Posts: 177
|
Posted: Tue Feb 20, 2007 1:56 pm Post subject: Gives null |
|
|
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 |
|
 |
Kishore Contributing Member

Joined: 20 Feb 2007 Posts: 202 Location: Banglore , India
|
Posted: Tue Feb 20, 2007 3:18 pm Post subject: |
|
|
please dump few duration records here. _________________ =================
Kish |
|
| Back to top |
|
 |
Kishore Contributing Member

Joined: 20 Feb 2007 Posts: 202 Location: Banglore , India
|
Posted: Tue Feb 20, 2007 3:23 pm Post subject: |
|
|
what is the duration field ? is it a datetime field? _________________ =================
Kish |
|
| Back to top |
|
 |
dotnetstarter Contributing Member

Joined: 19 Feb 2007 Posts: 177
|
Posted: Tue Feb 20, 2007 3:23 pm Post subject: Sample data |
|
|
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 |
|
 |
Kishore Contributing Member

Joined: 20 Feb 2007 Posts: 202 Location: Banglore , India
|
Posted: Tue Feb 20, 2007 3:31 pm Post subject: |
|
|
Can you try calldate instead of duration in the query for a test? _________________ =================
Kish |
|
| Back to top |
|
 |
dotnetstarter Contributing Member

Joined: 19 Feb 2007 Posts: 177
|
Posted: Tue Feb 20, 2007 3:34 pm Post subject: |
|
|
Gives
.000000 |
|
| Back to top |
|
 |
Kishore Contributing Member

Joined: 20 Feb 2007 Posts: 202 Location: Banglore , India
|
Posted: Tue Feb 20, 2007 3:36 pm Post subject: |
|
|
what data type is duration field? _________________ =================
Kish |
|
| Back to top |
|
 |
dotnetstarter Contributing Member

Joined: 19 Feb 2007 Posts: 177
|
Posted: Tue Feb 20, 2007 3:40 pm Post subject: |
|
|
Duration field is nvarchar
CallDate field is smalldatetime |
|
| Back to top |
|
 |
Kishore Contributing Member

Joined: 20 Feb 2007 Posts: 202 Location: Banglore , India
|
Posted: Tue Feb 20, 2007 3:42 pm Post subject: |
|
|
ok let me try something else. i'll test and tell you _________________ =================
Kish |
|
| Back to top |
|
 |
dotnetstarter Contributing Member

Joined: 19 Feb 2007 Posts: 177
|
Posted: Tue Feb 20, 2007 3:47 pm Post subject: New |
|
|
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 |
|
 |
dotnetstarter Contributing Member

Joined: 19 Feb 2007 Posts: 177
|
Posted: Tue Feb 20, 2007 3:58 pm Post subject: Got it worked |
|
|
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 |
|
 |
Kishore Contributing Member

Joined: 20 Feb 2007 Posts: 202 Location: Banglore , India
|
Posted: Tue Feb 20, 2007 4:18 pm Post subject: |
|
|
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 |
|
 |
|