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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Retrieve counter totals with rollover
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

consol
Starting Member

3 Posts

Posted - 07/12/2013 :  08:56:05  Show Profile  Reply with Quote
Hi,
I have limited knowledge of SQL.

I am totalising water flow.
I use a PLC (programmable logic controller) to increment a counter every time the flowmeter sends a pulse.
I have an application that logs this incrementing total every 10 sec to a table.
At midnight, the PLC resets the counter to zero.

I end up with a table containing a daily flow total.

I use this flow total in production reports.
I record, in another table, the start/end times of various production processes.
Using these times, I can calculate the total amount of water used in a process, (Total at 'End time' minus Total at 'Start time'), in a day.

My problem is when a process runs over a number of days.
How do I account for the reset at midnight (rollover)?

I need to get the delta change of the water total from the start of a process to the end of a process a few days later.

At the moment I'm doing the following:

Get value at start time/date (Vs)
Get max value for each day between start and finish of process (Vm)
Get value at end time/date (Ve)

So for a total running over 3 days:
Total = (Ve + Vm1 + Vm2) - Vs

How can I write a query to do this for me.
There are actually 30 flow meters in the process.

Basically, I want to input a start date/time and end date/time and return the total flow through each meter. (and allow for the reset of each meter at midnight)

Thanks for any help with this. (It's wrecking my head!)
Con

James K
Flowing Fount of Yak Knowledge

3760 Posts

Posted - 07/12/2013 :  09:47:27  Show Profile  Reply with Quote
Something along the lines shown below. You may need to add the meterId etc. in your query, but the basic idea is that you find each day's flow (in the inner query) and then sum them up in the outer query:
DECLARE @startDate DATETIME = '2013-07-12 09:12:28.150';
DECLARE @endDate DATETIME = '2013-07-15 13:28:31.000';

SELECT SUM(MaxVal-MinVal) FROM 
(
	SELECT
		CAST(YourDateTimeCol AS DATE) AS Date,
		MAX(VALUE) AS MaxVal,
		MIN(VALUE) AS MinVal
	FROM
		YourTable
	WHERE
		YourDateTimeCol >= @startDate
		AND YourDateTimeCol <= @endDate
	GROUP BY
		CAST(YourDateTimeCol AS DATE)
) s
Go to Top of Page

consol
Starting Member

3 Posts

Posted - 07/12/2013 :  10:20:28  Show Profile  Reply with Quote
Thanks James,
That's a neater method than my convoluted mess!

How would I structure this to return multiple meter totals in one table?
Thanks again for your help
Con
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3760 Posts

Posted - 07/12/2013 :  10:30:55  Show Profile  Reply with Quote
You can add the meterId also to the query:
DECLARE @startDate DATETIME = '2013-07-12 09:12:28.150';
DECLARE @endDate DATETIME = '2013-07-15 13:28:31.000';

SELECT meterId, SUM(MaxVal-MinVal) FROM 
(
	SELECT
		CAST(YourDateTimeCol AS DATE) AS Date,
		meterId,
		MAX(VALUE) AS MaxVal,
		MIN(VALUE) AS MinVal
	FROM
		YourTable
	WHERE
		YourDateTimeCol >= @startDate
		AND YourDateTimeCol <= @endDate
	GROUP BY
		CAST(YourDateTimeCol AS DATE),
		meterId
) s
GROUP BY meterId;
Go to Top of Page

consol
Starting Member

3 Posts

Posted - 07/12/2013 :  10:43:13  Show Profile  Reply with Quote
James,
Thanks very much for such a neat solution.
Appreciate it.
Con
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3760 Posts

Posted - 07/12/2013 :  12:40:24  Show Profile  Reply with Quote
You are very welcome - glad to help.
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.05 seconds. Powered By: Snitz Forums 2000