tags:

views:

48

answers:

2

I have a water pump that the run state is stored in a mssql database every 10 seconds along with the time of day. The pump cycles often through out the day. How do I query for the total time the pump was on during the day?

+3  A: 
SELECT COUNT(*) FROM pump WHERE state = on

Take the result, and multiply by 10 to get time on in seconds?

As mentioned in the comments, if you also select the date and GROUP BY date you can get counts per day.

Stefan Kendall
+1 Utterly, brilliantly simple. I think...
gbn
Well this would give him the total count, not per day. you would need to do some Grouping
Jack Marchetti
+1  A: 

Group by day, count the number of records, and multiply by 10:

select 
   year = datepart(y,datefield)
,  dayofyear = datepart(dy,datefield)
,  minuteson = count(*)*10
from pump
where state = 'on'
group by datepart(y,datefield), datepart(dy,datefield)
Andomar