473,387 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

compare date/time values in the same table

I have a table that is a project. Each record is a task in the
project. One field in each record is a date/time stamp for when that
task was completed. I need to be able to: 1) compute how much time has
passed between each date/time stamp and 2) compute how much time has
passed between the first and last date/time stamp (total project time).
This involves a self join, and I have nearly gotten it to work, but I
need a little more help.

Here is an example of the "selfJoin" table:

task_ID task date_time
1 go to grocery store 2004-12-01 15:53:55
2 cook food 2004-12-03 12:00:00
3 serve food 2004-12-03 18:00:00

Here is the best SQL statement I can come up with:

$sql = "SELECT first.task_ID, first.task, first.date_time,
UNIX_TIMESTAMP(second.date_time) - UNIX_TIMESTAMP(first.date_time)
FROM selfJoin AS first, selfJoin AS second
WHERE first.task_ID < second.task_ID";

Here is what I want the result to look like:

task_ID task date_time total time
1 go to grocery store 2004-12-01 15:53:55
2 cook food 2004-12-03 17:00:00 197065 seconds
3 serve food 2004-12-03 18:00:00 3600 seconds

Total project time: 201965 seconds

Except my SQL statement doesn't produce this. (I have PHP functions to
reformat the times to a nice format.)

What SQL statement determines the difference between a task's date_time
value and the one before it? Ie, how long each task took?
Thanks,
-Andrew K.

Jul 20 '05 #1
5 8573
ak****@web4000.com wrote:
task_ID task date_time total time
1 go to grocery store 2004-12-01 15:53:55
2 cook food 2004-12-03 17:00:00 197065 seconds
3 serve food 2004-12-03 18:00:00 3600 seconds What SQL statement determines the difference between a task's date_time
value and the one before it? Ie, how long each task took?


Select all required rows with columns and select datetimes using
UNIX_TIMESTAMP() function in MySQL. Then calculate the differences in
PHP. It might be possible to do that in MySQL, but it is far more
simpler to do that on PHP and that is how we do that at work for example.
Jul 20 '05 #2

<ak****@web4000.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I have a table that is a project. Each record is a task in the
project. One field in each record is a date/time stamp for when that
task was completed. I need to be able to: 1) compute how much time has
passed between each date/time stamp and 2) compute how much time has
passed between the first and last date/time stamp (total project time).
This involves a self join, and I have nearly gotten it to work, but I
need a little more help.

Here is an example of the "selfJoin" table:

task_ID task date_time
1 go to grocery store 2004-12-01 15:53:55
2 cook food 2004-12-03 12:00:00
3 serve food 2004-12-03 18:00:00

Here is the best SQL statement I can come up with:

$sql = "SELECT first.task_ID, first.task, first.date_time,
UNIX_TIMESTAMP(second.date_time) - UNIX_TIMESTAMP(first.date_time)
FROM selfJoin AS first, selfJoin AS second
WHERE first.task_ID < second.task_ID";

Here is what I want the result to look like:

task_ID task date_time total time
1 go to grocery store 2004-12-01 15:53:55
2 cook food 2004-12-03 17:00:00 197065 seconds
3 serve food 2004-12-03 18:00:00 3600 seconds

Total project time: 201965 seconds

Except my SQL statement doesn't produce this. (I have PHP functions to
reformat the times to a nice format.)

What SQL statement determines the difference between a task's date_time
value and the one before it? Ie, how long each task took?
Thanks,
-Andrew K.


This is a tricky query. First, it's easy to get the difference of row n and
row n+1. But's it's hard to display the difference in row n. I might have a
solution, but could you post you CREATE TABLE statements and some sample
INSERTS? And also, what time interval are you looking for between the tasks
? i.e minutes, hours, days, weeks, etc.

Best Regards,
Rich
Jul 20 '05 #3
I was on Christmas vacation and am just getting back to this. I am not
sure why you need a create table or insert statement - I think the
example table shows you everything you need to know. Table:

task_ID task date_time
1 plan meal 2004-12-01 9:53:00
2 go to grocery store 2004-12-01 15:53:55
3 cook food 2004-12-03 12:00:00
4 serve food 2004-12-03 18:00:00

What SQL statement shows me how many seconds have passed between tasks
2 & 1, 3 & 2 and 4 & 3 for n number of tasks?

What SQL statement shows me how many seconds have passed between tasks
1 and the final task?

Thanks,
-AK

Jul 20 '05 #4
ak****@web4000.com wrote:
I was on Christmas vacation and am just getting back to this. I am not
sure why you need a create table or insert statement - I think the
example table shows you everything you need to know. Table:

task_ID task date_time
1 plan meal 2004-12-01 9:53:00
2 go to grocery store 2004-12-01 15:53:55
3 cook food 2004-12-03 12:00:00
4 serve food 2004-12-03 18:00:00

What SQL statement shows me how many seconds have passed between tasks
2 & 1, 3 & 2 and 4 & 3 for n number of tasks?
SELECT T.task_ID, T.task, T.date_time,
COALESCE(PrevT.task_ID),
UNIX_TIMESTAMP(T.date_time) - UNIX_TIMESTAMP(PrevT.date_time),
NULL) AS TaskDuration
FROM selfJoin AS T LEFT OUTER JOIN selfJoin AS PrevT
ON PrevT.task_ID = T.task_ID - 1
What SQL statement shows me how many seconds have passed between tasks
1 and the final task?


SELECT UNIX_TIMESTAMP(LaterT.date_time) -
UNIX_TIMESTAMP(EarlierT.date_time) AS TotalProjectTime
FROM selfJoin AS EarlierT INNER JOIN LaterT
WHERE EarlierT.task_ID = (SELECT MIN(task_ID) FROM selfJoin)
AND LaterT.task_ID = (SELECT MAX(task_ID) FROM selfJoin)

Regards,
Bill K.
Jul 20 '05 #5

<ak****@web4000.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
I was on Christmas vacation and am just getting back to this. I am not
sure why you need a create table or insert statement - I think the
example table shows you everything you need to know. Table:

task_ID task date_time
1 plan meal 2004-12-01 9:53:00
2 go to grocery store 2004-12-01 15:53:55
3 cook food 2004-12-03 12:00:00
4 serve food 2004-12-03 18:00:00

What SQL statement shows me how many seconds have passed between tasks
2 & 1, 3 & 2 and 4 & 3 for n number of tasks?

What SQL statement shows me how many seconds have passed between tasks
1 and the final task?

Thanks,
-AK


In general, to get intra-row differences :

create table diffs(
col1 int);
insert into diffs values(3),(4),(9),(26);

select d1.col1,
(select min(d2. col1) - d1.col1 from diffs as d2 where d2.col1 >d1.col1)
from diffs as d1

To do the same thing with datetimes:

create table TimeDiffs(
col1 datetime);

/* execute the following, waiting a few seconds between executions */
insert into TimeDiffs values(now())

select d1.col1,
(select UNIX_TIMESTAMP(min(d2. col1))- UNIX_TIMESTAMP(d1.col1) from
TimeDiffs as d2 where d2.col1 >d1.col1)
from TimeDiffs as d1

To get the difference between the last task and the first task (assuming
each task starts later than the previous on:

select UNIX_TIMESTAMP(max(col1)) - UNIX_TIMESTAMP(min(col1)) from TimeDiffs

The only problem I have with your spec is that all you're getting is the
time between the start time of a task and the start time of the next task
(if date_time is the start date. It could be the end date). You're not
getting the duration of each task unless each task starts immediately after
the previous task (not likely).

Also, if you store date_time as a timestamp, then you'll lose it if you
update task.

I looked at Bill's response but didn't quite understand it, so maybe I don't
really understand what you're looking for.

Hope this helped--

Regards,
Rich
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Gleep | last post by:
Hey Guys, I've got a table called Outcomes. With 3 columns and 15 rows 1st col 2nd col 3rdcol outcome date price There are 15 rows...
8
by: peashoe | last post by:
I have an asp page that uses a calendar.js (pop-up) file to add an exact date format in the text field (txtDDate). My problem is I need some javascript that sets an alert that does not allow them...
9
by: Thomas R. Hummel | last post by:
Hello, I am importing data that lists rates for particular coverages for a particular period of time. Unfortunately, the data source isn't very clean. I've come up with some rules that I think...
5
by: Megan | last post by:
Hi everybody- I'm helping a friend with a music database. She has an old one and is creating a new one. She wants to compare records and fields in the old database with records and fields in the...
0
by: atif | last post by:
how to compare two dates n how to fill the text box with date only. i tried to do it like date.now() but the textbox is filled with date n time. Second questoin is i hav a table like "user" in the...
5
by: rcolby | last post by:
Evening, Wondering if someone can point me in the right direction, on how I would compare a system.guid with a system.byte. system.guid (pulled from sql server table with a data type of...
12
by: conckrish | last post by:
Hi all.. Can anyone tell me how to compare datetime objects?I ve three objects namely Current date,start date and end date.. I need to check the current date with Start date and end date....Plz...
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
6
osward
by: osward | last post by:
Hi everyone, I have query(s) that I need to perform which I couldn't figure out, and following is the situation I have event table (which stores the event details) that has eid, date, time,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.