Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Guys,

I have a problem. I want to find time diff between row values when their value is changed. I have the following similar data in my table, For ex:

CSS
------------------------------------
    DateandTime     |    DCStatus
------------------------------------
2012-01-01 01:00:00 |    Using
2012-01-02 02:00:00 |    Using
2012-01-03 03:00:00 |    Using
2012-01-04 04:00:00 |    Charging
2012-01-05 05:00:00 |    Charging
2012-01-06 06:00:00 |    Using
2012-01-06 07:00:00 |    Using
2012-01-06 08:00:00 |    Using
2012-01-05 09:00:00 |    Charging
2012-01-05 10:00:00 |    Charging
2012-01-05 11:00:00 |    Charging
------------------------------------


From this given database How can I calculate the average charging time and discharge time? I am very new to SQL programming. Please help me!!
Posted
Comments
enhzflep 14-Aug-12 4:15am    
Just to clarify, am I right in assuming this is the desired result?

Avg Using time = (3 + 3) / 2 = 3Hrs
Avg Charge time = (2 + 3) / 2 = 2.5 Hrs
RostocKrishna 14-Aug-12 4:33am    
Yes enhzflep, That is what exactly I want and I couldn't get it done.
enhzflep 14-Aug-12 5:34am    
Okay, then in that case, I'd question the structure of the table.
The first thing that comes to mind is that if the device status at 8:00:00 is Using, but at 8:00:01 the status changes to Charging, it's not picked up for another 59 mins and 59 secs - virtually an hour.

This in itself will introduce a big margin of error in any final avaerages (on the presented data set - longer durations of monitoring wont be affected as much by a single event like this, though they may have many, many events like the one I describe)

The other thing that comes to mind is the way the Date/Time is stored. It appears to be stored as a text string that represents the time the status is checked. I imagine you're using something like date('Y-m-d') to get the time that's entered into a string field in the database. I prefer to use time(), and insert an integer of the number of seconds elapsed since January 1 1970 00:00:00 GMT

This allows you to do very easy maths on the time - calculating the difference in time between two rows then becomes trivial. As it stands, my understamding is that you have to (a) extract the DateandTime value (b) convert it to something more useful using sscanf or something similar, before
(c) performing the math on two date/times.

Storing as an int of number of seconds elapsed is the way that forums are able to say "Posted 1hr ago" or "Posted 8 mins ago" without having to do lots of messy conversion. You simply convert whatever you're going to display and that's it.

I also notice that there are events spread out over a number of days. This also complicates things - it's unclear whether sampling occurs on the hour when in use or being charged, or if it occurs at some arbitrary (unspecified) time.

Right now, I'm thinking that you'll need to extract the raw data using MySQL before examining it with code to determine how many, the length of and the status of each contiguous block of rows, as determined by the DCStatus column.
RostocKrishna 14-Aug-12 6:01am    
Hello enhzflep,

Thank you for the advices.

Actually, the database is updated at a regular interval of 10 secs.

And the date and time is time stamp which is set to current timestamp.
enhzflep 16-Aug-12 19:36pm    
No worries. :) The other thing worth considering is the fact that you don't know *when* the status changes from using -> charging or vise-versa.
It may be after 10% of the time between samples has elapsed, it may be after 99% of the time has elapsed. In any case, you don't know...

Looking your sample data above - if the events are all set to occur 1 second apart on the same day, we realize that there's 4 contiguous blocks. There are 3 instances when the status changes - in each case we don't really know whether we should mark the period as Using or Charging. Perhaps you may choose to mark these periods as being 50% using & 50% charging.

Here's the rough output that explains this further (I did need to step through the SQL query results to calculate the time blocks)

Date Time Status
------------------------------
2012-01-01 01:00:00 Using
2012-01-01 01:00:01 Using
2012-01-01 01:00:02 Using
--unknown status--
2012-01-01 01:00:03 Charging
2012-01-01 01:00:04 Charging
--unknown status--
2012-01-01 01:00:05 Using
2012-01-01 01:00:06 Using
2012-01-01 01:00:07 Using
--unknown status--
2012-01-01 01:00:08 Charging
2012-01-01 01:00:09 Charging
2012-01-01 01:00:10 Charging
===========================
Num determinate blocks: 4
===========================
2012-01-01 01:00:00 - 2012-01-01 01:00:02 - Using (2 seconds)
2012-01-01 01:00:03 - 2012-01-01 01:00:04 - Charging (1 seconds)
2012-01-01 01:00:05 - 2012-01-01 01:00:07 - Using (2 seconds)
2012-01-01 01:00:08 - 2012-01-01 01:00:10 - Charging (2 seconds)

===========================
Num indeterminate blocks: 3
===========================
2012-01-01 01:00:02 - 2012-01-01 01:00:03 - unknown (1 seconds)
2012-01-01 01:00:04 - 2012-01-01 01:00:05 - unknown (1 seconds)
2012-01-01 01:00:07 - 2012-01-01 01:00:08 - unknown (1 seconds)

Total Time: 10s (100%)
Unknown: 3s (30%)
Using: 4s (40%)
Charging 3s (30%)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900