Forum Moderators: open

Message Too Old, No Replies

Statistical (Average) calculations with SQL Server?

How to do calculations using rowns instead of columns

         

NooK

8:05 am on Apr 22, 2008 (gmt 0)

10+ Year Member



I have a log table which has a few columns

[caseID] **Int**
[Type] **Int**
[SubType] **Int**
[time] **datetime**

This table just logs information where the first column just logs the id of a certain case, the next 2 columns the type and subtype of the event, and the time column logs when that event has happened for that case.

For example you can have:

1 0 0 "12/04/08 12:35:00"
1 0 1 "12/04/08 12:39:00"
1 1 2 "12/04/08 12:51:00"
2 0 0 "12/04/08 12:35:20"
2 1 2 "15/04/08 16:12:20"

Is it possible to do some statistical calculations on that table using the rows, for example, calculating the average time it takes to go between type and subtype 0,0 to type and subtype 1,2?

I know you can calculate average and such between columns but being new to SQL I am not sure where to start in a case like this where rows are what I am using.

Any input is welcome.

NooK

11:15 am on May 12, 2008 (gmt 0)

10+ Year Member



Is there nobody that can help.

What I need is basically, be able to take AVG((SELECT column A WHERE column B=X) - (SELECT column A WHERE column B=Y)).

Is this doable? Basically taking the difference of 2 values which are in different rows but share a common value (Like ID column) and then do an AVG for that difference over all different IDs.

vincevincevince

11:25 am on May 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



SELECT (SUM(`A`)/COUNT(`A`)) WHERE `B` = Y

That would give you the average of all A where B = Y for the row. Should put you on the right lines.

NooK

11:08 am on May 15, 2008 (gmt 0)

10+ Year Member



Hmm not exactly sure it does it but in the end after I researched a bit I ended up finding the solution

Created a view with the first and second query and then did a AVG(DATEDIFF)) between them and it worked.

Thanks