views:

245

answers:

3

Details. I have the notes table having the following columns.

ID       - INT(3)
Date     - DateTime
Note     - VARCHAR(100)
Tile     - Varchar(100)
UserName -  Varchar(100)

Now this table will be having NOTES along with the Titles entered by UserName on the specified date / time.

I need to calculate the DateTimeDifference between the TWO ROWS in the SAME COLUMN

For example the above table has this peice of information in the table.

64, '2010-03-26 18:16:13', 'Action History', 'sending to Level 2.', 'Salman Khwaja'
65, '2010-03-26 18:19:48', 'Assigned By', 'This is note one for the assignment of RF.', 'Salman Khwaja'
66, '2010-03-27 19:19:48', 'Assigned By', 'This is note one for the assignment of CRF.', 'Salman Khwaja'

Now I need to have the following resultset in query reports using MYSQL.

TASK                -  TIME Taken
ACTION History      - 2010-03-26 18:16:13
Assigned By         - 00:03:35
Assigned By         - 25:00:00

More smarter approach would be

TASK                -  TIME Taken
ACTION History      - 2010-03-26 18:16:13
Assigned By         - 3 minutes 35 seconds
Assigned By         - 1 day, 1 hour.

I would appreciate if one could give me the PLAIN QUERY along with PHP code to embed it too.

+1  A: 
<?php
$start = new DateTime('2009-01-01 00:00:00'); // 31 days
$time_span = $start->diff(new DateTime('2009-02-01 00:00:00'));
var_dump($time_span); // returns '1 month'

$start = new DateTime('2009-02-01 00:00:00'); //28 days
$time_span = $start->diff(new DateTime('2009-03-01 00:00:01'));
var_dump($time_span); // returns '1 month'
?>
Svisstack
http://php.net/manual/en/datetime.diff.php
Svisstack
thanks but I would appreciate the MYSQL Query as well.
Stringz
A: 

DATEDIFF()

Phill Pafford
I have searched and found it, but I just can't implement it in the Query. Can you please help me out.Thanks.
Stringz
What do you want the query to return? how do I choose the two rows to compare the dates? is it just compare the next rows date with the previous row?
Phill Pafford
Yes.What I need is to compare the Date / Time in one Column (CurrentDate) of Row one to row two. Compare it and give the difference in human readable form, which is one day, 2 hours, 23 minutes. For details please see the snapshots.http://processmaker.wordpress.com/2010/04/20/query-issues/dump1/http://processmaker.wordpress.com/2010/04/20/query-issues/
Stringz
A: 

It looks like you want to group by case number.

Using your schema and sample data, I think that this is exactly what you wanted:

SELECT  t1.ID, t1.title AS task, t1.username,
  IFNULL(CONCAT(TIMESTAMPDIFF(MINUTE, t2.currentDate, t1.currentDate)), t1.currentdate) AS time_taken
FROM tps_trans_support_notes t1
LEFT JOIN tps_trans_support_notes t2
  ON t2.currentdate < t1.currentdate AND
    t2.ID <> t1.ID AND
    t2.casenumber = t1.casenumber
LEFT JOIN tps_trans_support_notes t3
  ON t3.casenumber = t1.casenumber AND
    t3.ID <> t1.ID AND t3.ID <> t2.ID AND
    t3.currentdate > t2.currentdate AND
    t3.currentdate < t1.currentdate
WHERE t3.ID IS NULL AND
  t1.casenumber = '21'
ORDER BY t1.ID

First, the query gets the begin time and end time into the same row, excluding rows where there are times that occur between the two, then it displays the difference.

The query only shows the difference in minutes, but you can use the other DateTime functions to expand that.

Marcus Adams
Thanks Marcus. This query works like a charm. But the thing is,This query is giving me the result by comparing the first time against all the other times (off course filtered by the criterion set in the ON clause.The time difference is coming in terms of from comparing first record (as benchmark) and the rest is compared to that first record.What I want is the the running interval between the two tasks. In my above example, compare ID 64 with 65, and then 65 with 66. Again, THANKYOU SO MUCH for the Solution. I would appreciate if you could provide the answer to this query also.
Stringz
@Stringz, sorry, I thought you wanted them grouped by user, title, and note. Do you just want the result set ordered by the Date column? You don't care that the previous row is for a different User, Title, or Note? Also, can the ID column be substituted for the Date column?
Marcus Adams
@ Marcus. I do want them grouped by User, Title, and Note. It is good, but the comparing part is that I need the interval between each date / time. The query you have provided is giving me interval from the first to second, then first to third, and first to fourth. While I need from First to Second, Second to Third, Third to Fourth, so on and so forth.Thanks again.
Stringz
@Stringz, sorry, that was a flaw in my query. I had forgotten to exclude rows where there were times between the begin and end date. I've updated the query to exclude those rows.
Marcus Adams
@ Marcus. Thanks Marcus. I am sharing you the Table Creation statements along with the changed query. Can you please update me with the right query.The table creation statements are better entered here.http://processmaker.wordpress.com/2010/04/20/query-issues/I would appreciate if you could correct my query which will give me the right results.
Stringz
@Stringz, I updated my answer for you schema and sample data.
Marcus Adams
@ Marcus. Awesome Query Marcus. Thank you so much. Can we make this query more human. The minutes displayed in the query, can we convert the minutes (if greater than 60) to one hour. And if hours is greater than 24, can we convert it to day. So for example, the result for "1500 minutes" could be displayed as "1 day 1 hour".
Stringz