SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Cumulative average based on row criteria
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

azoo123
Starting Member

2 Posts

Posted - 02/04/2013 :  17:17:28  Show Profile  Reply with Quote
Hey all,

I am trying to go through this table, and then for every change in LOC_Zone, record the average amount of time that a certain loczone takes up. For example, for the UTILGLAVA loc_zone I would need to sum the amount of time that has passed until the next loc zone appears (in this case 29102). I would then sum the amount of time spent in 29102 until the next loc zone (integeosc). The process would repeat with the average time spent in each loc_zone being my final desired result.

Here is an image of the table for clarification:

http://imgur.com/m8h7gQJ]


Is this possible to do in sql alone, or do I need to use some other scripting language?
Thanks for your help!

-Andrew

James K
Flowing Fount of Yak Knowledge

3742 Posts

Posted - 02/04/2013 :  17:34:46  Show Profile  Reply with Quote
How do you calculate the time spent in a given zone? In your screen shot I see a LOC_DATE; is that what needs to be used? For example, if you take LOC_ZONE = 29102, which has just one time stamp, 2011-06-15 04:06:57, what is the average time for that LOC_ZONE? Perhaps the difference between the next timestamp (for INTEGEOSC) and the one timestamp for LOC_ZONE?

It would add a lot of clarity if you posted what the expected result for the sample data is.
Go to Top of Page

TG
Flowing Fount of Yak Knowledge

USA
6062 Posts

Posted - 02/04/2013 :  17:42:05  Show Profile  Reply with Quote
quote:
Originally posted by James K
It would add a lot of clarity if you posted what the expected result for the sample data is.


Indeed - have a look here:
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Be One with the Optimizer
TG
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000