self join

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew

    #1

    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_tim e) 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
  • Bill Karwin

    #2
    Re: self join

    Andrew wrote:[color=blue]
    > 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_tim e) FROM selfJoin
    > AS first, selfJoin AS second";[/color]

    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_tim e)
    - 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.

    Comment

    • Azeus

      #3
      Re: self join


      "Andrew" <akoper@web4000 .com> wrote in message
      news:42cdd29b.0 411291849.460c9 512@posting.goo gle.com...[color=blue]
      > 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_tim e) FROM selfJoin
      > AS first, selfJoin AS second";
      >
      > What is the correct way to do this?
      >[/color]

      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:s econds:millisec onds

      But only from MySql 4.1.1

      Check http://dev.mysql.com/doc/mysql/en/Da...functions.html


      Comment

      Working...