Author |
Topic |
musbdc
Starting Member
5 Posts |
Posted - 2013-06-10 : 04:32:43
|
Hi,I will really appreciate if you could help me to solve a very complicated problem.table: machinedataID CODE(1=working,0=stopped) dt1 1 2013-06-01 22:12:232 1 2013-06-02 20:11:113 0 2013-06-03 12:02:454 1 2013-06-04 04:35:255 0 2013-06-05 09:42:016 0 2013-06-05 17:10:10I need to find the total working and stopage time for the machine.One of the difficulty, it may be 10 records with code 1 or 0 in sequense with different times.Many thanks for your time.chris |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-06-10 : 04:48:42
|
what should be the output for above sample data?--Chandu |
 |
|
musbdc
Starting Member
5 Posts |
Posted - 2013-06-10 : 04:56:32
|
quote: Originally posted by bandi what should be the output for above sample data?Thanks.--Chandu
codechanges total(seconds)worked total(seconds)stopped4 times 32000 6500manually calculated result should be similar to the following:worked 2013-06-01 22:12:23 - 2013-06-02 20:11:11 = 79128 secondsworked 2013-06-02 20:11:11 - 2013-06-03 12:02:45=57094 secondsstopped 2013-06-03 12:02:45 - 2013-06-04 04:35:25 = 59560 secondsworked 2013-06-04 04:35:25 - 2013-06-05 09:42:01 = 104796 secondsstopped 2013-06-05 09:42:01 - 2013-06-05 17:10:10= 26899 secondsexpected outputchanges totalworked totalstopage4 79128 + 57094 +104796 =241018 59560 + 26899 =86459 |
 |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
|
musbdc
Starting Member
5 Posts |
Posted - 2013-06-10 : 05:59:59
|
quote: Originally posted by bandi Is there any other columns like datetime/any ?Tell us the logic behind the expected result...Do you want sum of records which has CODE=1 and sum of records which has CODE=0?Have a look at this linkhttp://www.sqlteam.com/article/working-with-time-spans-and-durations-in-sql-server--Chandu
Hi,I am afraid it is not a simple sum of 1 or 0s.we have to find the totals up to a statu changes ocur.when it is 1 we find the totals but it extends to the next line, 0.We have to find the total seconds for 1 and 0 at the end.Many thanks for the link. But We have no end date for our case. We have to get the end date next record ahead when the changes ocur.in our sample the start date for 1 is 2013-06-01 22:12:23 and the end date is 2013-06-03 12:02:45 when the status became 0. |
 |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-06-10 : 06:38:50
|
how come the column Changes is 4 ?;WITH CTE AS(SELECT ID, CODE, MinDate AS timeOut, MAX(dt) TimeInFROM machinheData tOUTER APPLY (SELECT MIN(dt) AS MinDate FROM machinheData WHERE dt > t.dt )t1GROUP BY ID, CODE, MinDate)SELECT SUM(CASE WHEN CODE = 1 THEN DATEDIFF( SS, TimeIn, TimeOut) END) WorkedSeconds ,SUM(CASE WHEN CODE = 0 THEN DATEDIFF( SS, TimeIn, TimeOut) END) WorkedSecondsFROM CTE --Chandu |
 |
|
musbdc
Starting Member
5 Posts |
Posted - 2013-06-10 : 07:00:52
|
quote: Originally posted by bandi how come the column Changes is 4 ?;WITH CTE AS(SELECT ID, CODE, MinDate AS timeOut, MAX(dt) TimeInFROM machinheData tOUTER APPLY (SELECT MIN(dt) AS MinDate FROM machinheData WHERE dt > t.dt )t1GROUP BY ID, CODE, MinDate)SELECT SUM(CASE WHEN CODE = 1 THEN DATEDIFF( SS, TimeIn, TimeOut) END) WorkedSeconds ,SUM(CASE WHEN CODE = 0 THEN DATEDIFF( SS, TimeIn, TimeOut) END) WorkedSecondsFROM CTE --Chandu
IT IS EXCELLENT AND WORKS PERFECTLY!!!. many many thanks. Could you please add someting to find the number of changes. I mean when goes 1 to 0 or 0 to 1 we say 1 changes happened. For our example there are four distinct changes: 1 - 0 - 1 - 0 (or three 0 - 1 - 0)We read data many times as 1 in sequence and we have to accept as only one changes until 0 comes. 1 to 0 = 1 changes. After several 0s may come. when it turns to 1 again, the second changes happens.So 1 turns to 0 is one change and when 0 turns to 1 is another change. We should not count the repeating 1s and 0s.Many thanks bandi. I hope you will do a last touch. |
 |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-06-10 : 08:05:00
|
[code];WITH CTE AS(SELECT ID ,CODE ,MinDate AS timeOut ,MAX(dt) TimeIn ,COUNT( distinct Grp) ChangeFROM machinheData tOUTER APPLY (SELECT MIN(dt) OVER() AS MinDate ,ROW_NUMBER() OVER(ORDER BY dt) - ROW_NUMBER() OVER(PARTITION BY code ORDER BY dt) AS Grp FROM machinheData WHERE dt > t.dt )t1GROUP BY ID, CODE, MinDate)SELECT MAX(Change) change ,SUM(CASE WHEN CODE = 1 THEN DATEDIFF( SS, TimeIn, TimeOut) END) WorkedSeconds ,SUM(CASE WHEN CODE = 0 THEN DATEDIFF( SS, TimeIn, TimeOut) END) WorkedSecondsFROM CTE[/code]--Chandu |
 |
|
musbdc
Starting Member
5 Posts |
Posted - 2013-06-10 : 08:18:29
|
Perfection!!! I donot know how to thank you. I hope I can generate some Money from this Project and share with you:)Best wishes.Chris |
 |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-06-10 : 08:41:17
|
quote: Originally posted by musbdc Perfection!!! I donot know how to thank you. I hope I can generate some Money from this Project and share with you:)Best wishes.Chris
Glad to be of........... --Chandu |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-11 : 02:00:25
|
quote: Originally posted by visakh16 sorry but isnt this just enough so far as only concrened thing is date sequence and the finding timedifference between the records;With CTEAS(SELECT ROW_NUMBER() OVER (ORDER BY dt) AS Seq,*FROM table)SELECT SUM(CASE WHEN c1.[Code] = 0 THEN DATEDIFF(ss,c1.[dt],c2.[dt]) ELSE 0 END) AS StoppedTime,SUM(CASE WHEN c1.[Code] = 1 THEN DATEDIFF(ss,c1.[dt],c2.[dt]) ELSE 0 END) AS WorkededTime,SUM(CASE WHEN c1.[Code] <> c2.[Code] THEN 1 ELSE 0 END) AS ChangesFROM CTE c1INNER JOIN CTE c2 ON c2.Seq = c1.Seq + 1 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-06-11 : 02:11:40
|
quote: Originally posted by visakh16 sorry but isnt this just enough so far as only concrened thing is date sequence and the finding timedifference between the records;With CTEAS(SELECT ROW_NUMBER() OVER (ORDER BY dt) AS Seq,*FROM table)SELECT SUM(CASE WHEN c1.[Code] = 0 THEN DATEDIFF(ss,c1.[dt],c2.[dt]) ELSE 0 END) AS StoppedTime,SUM(CASE WHEN c1.[Code] = 1 THEN DATEDIFF(ss,c1.[dt],c2.[dt]) ELSE 0 END) AS WorkededTime,SUM(CASE WHEN c1.[Code] <> c2.[Code] THEN 1 ELSE 0 END) AS ChangesFROM CTE c1INNER JOIN CTE c2 ON c2.Seq = c1.Seq + 1 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
Nice visakh... --Chandu |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-11 : 02:16:48
|
quote: Originally posted by bandi
Nice visakh... --Chandu[/quote]Thx ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
|