Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Calculating duration of on/off

$
0
0

I'm sure this has been answered out there, I just can't seem to find the right search condition

DECLARE @OrderRanking as table ( Orderdate datetime NOT NULL , Duration int , durstatus int ) INSERT @OrderRanking (Orderdate, Duration, durstatus) SELECT '2014-03-10 10:55:12.000', 1000, 1 UNION SELECT '2014-03-11 10:55:12.000', 500, 1 UNION SELECT '2014-03-14 10:55:12.000', 650, 1 UNION SELECT '2014-03-15 10:55:12.000', 3000, 0 UNION SELECT '2014-03-17 10:55:12.000', 1000, 1 UNION SELECT '2014-03-17 11:55:12.000', 2000, 1 UNION SELECT '2014-03-18 10:55:12.000', 500, 1 UNION SELECT '2014-03-20 10:55:12.000', 500, 0 UNION SELECT '2014-03-21 10:55:12.000', 500, 0 select * FROM @orderranking

--what I'm going for

SELECT '2014-03-10 10:55:12.000', 2150, 1 UNION SELECT '2014-03-15 10:55:12.000', 3000, 0 UNION SELECT '2014-03-17 10:55:12.000', 3500, 1 UNION SELECT '2014-03-20 10:55:12.000', 1000, 0


Essentially I need to sum up the duration each time the dur status flag switches, easy to do with a cursor, but I'm sure there's a CTE/ranking type solution out there. I'm on SQL2005 at the moment.

Best Wishes, The Redman; If something helps, please help show it by voting, if it solves, bonus!



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>