Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
jay_t
Starting Member
1 Post |
Posted - 2009-10-22 : 13:47:49
|
I am trying to calculate the total volume of water irrigated over a section of land. What I have is the change of flow at an instant of time recorded into a SQL database. -this is measured in cubic meters per hour.Date_Time Flow_Value2009/10/22 04:00:00.0 02009/10/22 04:00:16.2 232009/10/22 04:00:20.6 342009/10/22 04:00:39.7 952009/10/22 04:00:41.7 972009/10/22 04:01:15.1 1102009/10/22 04:03:17.0 952009/10/22 04:06:53.8 822009/10/22 04:26:50.7 772009/10/22 04:36:50.8 762009/10/22 04:46:51.7 722009/10/22 04:56:52.2 742009/10/22 05:16:52.7 722009/10/22 05:26:53.2 702009/10/22 05:36:22.1 842009/10/22 05:46:16.3 812009/10/22 05:56:16.2 752009/10/22 06:16:17.3 732009/10/22 06:26:16.9 752009/10/22 06:36:17.7 712009/10/22 06:57:38.7 572009/10/22 06:57:48.9 442009/10/22 06:57:53.4 282009/10/22 06:57:55.3 122009/10/22 07:07:55.1 0 Its simply not the case jut to sum up the values and assume that is the total volume of water irrigated.what needs to be done is work out the time difference per time stamp and calculate the volume for that time duration, and then have it over the hour(s) the user has selected.so for the above data, then the time difference would be (for the first hour)time_diff volume00:00:04.4 101.2000:00:19.1 649.4000:00:02.0 190.0000:00:33.5 3249.5000:02:01.9 13409.0000:03:36.8 20596.0000:19:56.9 98145.8000:10:00.1 46207.7000:10:00.9 45668.4000:10:00.5 43236.0000:20:00.5 88837.0000:10:00.5 13521.60 There for the total volume irrigated of that hour (from 4am to 5am) is : 373811.6 cubic meter's of water divided by 3600 = 103.8365556The question is: How do I do this with SQL - I am totally lost, and do not know where to begin, any help would be appreciated |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2009-10-22 : 14:07:34
|
| I'm not sure how you're calculating the Volume, but here's the time differences. The formual for the volume should be easy enough to calculate.DECLARE @TABLE TABLE(Date_Time datetime,Flow_Value float)INSERT INTO @TableSELECT '2009/10/22 04:00:00.0', 0 UNION ALLSELECT '2009/10/22 04:00:16.2', 23 UNION ALLSELECT '2009/10/22 04:00:20.6', 34 UNION ALLSELECT '2009/10/22 04:00:39.7', 95 UNION ALLSELECT '2009/10/22 04:00:41.7', 97 UNION ALLSELECT '2009/10/22 04:01:15.1', 110 UNION ALLSELECT '2009/10/22 04:03:17.0', 95 UNION ALLSELECT '2009/10/22 04:06:53.8', 82 UNION ALLSELECT '2009/10/22 04:26:50.7', 77 UNION ALLSELECT '2009/10/22 04:36:50.8', 76 UNION ALLSELECT '2009/10/22 04:46:51.7', 72 UNION ALLSELECT '2009/10/22 04:56:52.2', 74 UNION ALLSELECT '2009/10/22 05:16:52.7', 72 UNION ALLSELECT '2009/10/22 05:26:53.2', 70 UNION ALLSELECT '2009/10/22 05:36:22.1', 84 UNION ALLSELECT '2009/10/22 05:46:16.3', 81 UNION ALLSELECT '2009/10/22 05:56:16.2', 75 UNION ALLSELECT '2009/10/22 06:16:17.3', 73 UNION ALLSELECT '2009/10/22 06:26:16.9', 75 UNION ALLSELECT '2009/10/22 06:36:17.7', 71 UNION ALLSELECT '2009/10/22 06:57:38.7', 57 UNION ALLSELECT '2009/10/22 06:57:48.9', 44 UNION ALLSELECT '2009/10/22 06:57:53.4', 28 UNION ALLSELECT '2009/10/22 06:57:55.3', 12 UNION ALLSELECT '2009/10/22 07:07:55.1', 0 ;WITH cte_TableAS( SELECT RowNumber = Row_Number() OVER(order by date_time) ,Date_Time ,Flow_Value FROM @table )SELECT [Time_Diff] = DATEDIFF(ms,t1.date_time,t2.date_time)FROM cte_table t1LEFT JOIN cte_table t2ON t1.RowNumber = t2.RowNumber - 1JimEveryday I learn something that somebody else already knew |
 |
|
|
|
|
|
|
|