473,387 Members | 1,534 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.

self join

I want to find the difference in times between the date/times of two
tasks in the same table.

selfJoin table
task_ID task date_time
1 measure board 2004-11-28 15:53:55
2 cut board 2004-11-28 16:08:03

Here is the best SQL statement I can come up with to find how much
time has passed between those two events:

$sql = "SELECT DIFF(first.date_time, second.date_time) FROM selfJoin
AS first, selfJoin AS second";

What is the correct way to do this?

(Never mind that there will in fact be many tasks in each table. And
I will also want to know the total time difference between the first
task and the last task.)

Thanks,
-Andrew K
Jul 20 '05 #1
2 2247
Andrew wrote:
I want to find the difference in times between the date/times of two
tasks in the same table.

selfJoin table
task_ID task date_time
1 measure board 2004-11-28 15:53:55
2 cut board 2004-11-28 16:08:03

Here is the best SQL statement I can come up with to find how much
time has passed between those two events:

$sql = "SELECT DIFF(first.date_time, second.date_time) FROM selfJoin
AS first, selfJoin AS second";


DIFF() is not a function in MySQL as far as I know.
Read the docs on date/time functions:
http://dev.mysql.com/doc/mysql/en/Da...functions.html

I would suggest this:

SELECT UNIX_TIMESTAMP(second.date_time)
- UNIX_TIMESTAMP(first.date_time)
FROM selfJoin AS first, selfJoin AS second
WHERE first.task_ID <> second.task_ID

This returns the difference in seconds between those two date/times,
assuming both are between 1970 and 2037. :-)

I'm assuming you left out any join condition because you want all rows
to be compared to all other rows. It's often handy to add a condition
as I have above, so you don't get each row compared to itself.

Regards,
Bill K.
Jul 20 '05 #2

"Andrew" <ak****@web4000.com> wrote in message
news:42**************************@posting.google.c om...
I want to find the difference in times between the date/times of two
tasks in the same table.

selfJoin table
task_ID task date_time
1 measure board 2004-11-28 15:53:55
2 cut board 2004-11-28 16:08:03

Here is the best SQL statement I can come up with to find how much
time has passed between those two events:

$sql = "SELECT DIFF(first.date_time, second.date_time) FROM selfJoin
AS first, selfJoin AS second";

What is the correct way to do this?


MySql offers several date/time functions

DATEDIFF and TIMEDIFF.

DATEDIFF Only returns the difference in days, while TIMEDIFF returns the
difference in the following format:

hours:minutes:seconds:milliseconds

But only from MySql 4.1.1

Check http://dev.mysql.com/doc/mysql/en/Da...functions.html
Jul 20 '05 #3

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

Similar topics

2
by: Tech | last post by:
I have a table tblEmails where the columns are id,list_id,address_id. I have many lists. I need to find out if a couple of lists (list_ids - 1000,1001,1002) have same address_ids in common or...
7
by: Andrew Mayo | last post by:
Here's a really weird one for any SQL Server gurus out there... We have observed (SQL Server 2000) scenarios where a stored procedure which (a) begins a transaction (b) inserts some rows into...
4
by: jbm05 | last post by:
Hi, I'm curious about the computational complexity of a query I have. The query contains multiple nested self left joins, starting with a simple select, then doing a self left join with the...
4
by: Shahzad | last post by:
dear respected gurus, I would like to knew how to apply append,insert query for a self table where no primary keys issues. i do have problem say there are 5 rows of single record, this is data...
6
by: davegb | last post by:
I'm trying to create a self-join table to show the relationship between employee and supervisor. In another thread, I was advised to create a SupervisorID in the employee table, a separate...
2
by: Ev | last post by:
I have a database table in SQL Server that has a self join. In C# I have a DataTable with a self-join. I have defined a foreign key constraint on the DataTable for the self join. The...
14
by: Jim Andersen | last post by:
I have a problem with this standard employee-supervisor scenario For pictures: http://www.databasedev.co.uk/self-join_query.html I want to show all employees "belonging" to a specific...
6
by: Russell Warren | last post by:
Is there any better way to get a list of the public callables of self other than this? myCallables = classDir = dir(self) for s in classDir: attr = self.__getattribute__(s) if callable(attr)...
3
by: sks | last post by:
I have a table that contains keywords (Varchars) each one mapped to a product. so the database schema is just an id column, product column and keyword column. I want to select the products that...
84
by: braver | last post by:
Is there any trick to get rid of having to type the annoying, character-eating "self." prefix everywhere in a class? Sometimes I avoid OO just not to deal with its verbosity. In fact, I try to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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.