Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL Query to find Timedifference between 2 items whose ID is same but different Status

SQL Query to find Timedifference between 2 items whose ID is same but different Status

Scheduled Pinned Locked Moved Database
databasequestion
10 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DrukenProgrammer
    wrote on last edited by
    #1

    Hello Experts, I have my data like below, ID Fname Date_TimeWorked Status 1 MyName 2006-10-25 10:00:05 Ready 1 MYName 2006-10-25 11:00:05 Completed 1 MyName 2006-10-25 10:30:00 I want to know the time difference Between Ready-completed, ready-canceled. How do I query this? Its urgent. Thanks in advance!!

    W 1 Reply Last reply
    0
    • D DrukenProgrammer

      Hello Experts, I have my data like below, ID Fname Date_TimeWorked Status 1 MyName 2006-10-25 10:00:05 Ready 1 MYName 2006-10-25 11:00:05 Completed 1 MyName 2006-10-25 10:30:00 I want to know the time difference Between Ready-completed, ready-canceled. How do I query this? Its urgent. Thanks in advance!!

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      For example:

      SELECT DATEDIFF(minute, start.Date_TimeWorked, end.Date_TimeWorked) AS ElapsedMinutes
      FROM TableName start,
      TableName end
      WHERE start.ID = end.ID
      AND start.Status = 'Ready'
      AND end.Status = 'Completed'

      Mika

      D 1 Reply Last reply
      0
      • W Wendelius

        For example:

        SELECT DATEDIFF(minute, start.Date_TimeWorked, end.Date_TimeWorked) AS ElapsedMinutes
        FROM TableName start,
        TableName end
        WHERE start.ID = end.ID
        AND start.Status = 'Ready'
        AND end.Status = 'Completed'

        Mika

        D Offline
        D Offline
        DrukenProgrammer
        wrote on last edited by
        #3

        I have this in one table, what is TableName start,TableName end? Thank you

        W 1 Reply Last reply
        0
        • D DrukenProgrammer

          I have this in one table, what is TableName start,TableName end? Thank you

          W Offline
          W Offline
          Wendelius
          wrote on last edited by
          #4

          Since I didn't know the actual name for your table I used only TableName. Let's say your table is named WorkData then the query would look like:

          ...
          FROM WorkData start,
          WorkData end
          ...

          Start and end are aliases for the same table so that it can be referred twice. I used start alias (the alias can be whatever you want) for the row that defines the starting point for time and respectively end for ending point. Hope this helps, Mika

          D 1 Reply Last reply
          0
          • W Wendelius

            Since I didn't know the actual name for your table I used only TableName. Let's say your table is named WorkData then the query would look like:

            ...
            FROM WorkData start,
            WorkData end
            ...

            Start and end are aliases for the same table so that it can be referred twice. I used start alias (the alias can be whatever you want) for the row that defines the starting point for time and respectively end for ending point. Hope this helps, Mika

            D Offline
            D Offline
            DrukenProgrammer
            wrote on last edited by
            #5

            I just created a simple access database for it and tried, but it is prompting me Minute.. Entere Parameter value. ID Fname Date_TimeWorked Status 1 MyName 2006-10-25 10:00:05 Ready 1 MYName 2006-10-25 11:00:05 Completed 1 MyName 2006-10-25 10:30:00 Cancled

            W 1 Reply Last reply
            0
            • D DrukenProgrammer

              I just created a simple access database for it and tried, but it is prompting me Minute.. Entere Parameter value. ID Fname Date_TimeWorked Status 1 MyName 2006-10-25 10:00:05 Ready 1 MYName 2006-10-25 11:00:05 Completed 1 MyName 2006-10-25 10:30:00 Cancled

              W Offline
              W Offline
              Wendelius
              wrote on last edited by
              #6

              The code was for SQL Server which most of the people here use. It's been ages since I last used Access, but try just subtracting the two times (end.Date_TimeWorked - start.Date_TimeWorked). I'm not sure but it may give you the result you want.

              D 1 Reply Last reply
              0
              • W Wendelius

                The code was for SQL Server which most of the people here use. It's been ages since I last used Access, but try just subtracting the two times (end.Date_TimeWorked - start.Date_TimeWorked). I'm not sure but it may give you the result you want.

                D Offline
                D Offline
                DrukenProgrammer
                wrote on last edited by
                #7

                Well I was playing with Excel sheets to database, so I was trying something on access database. and thank you for your reply, the query looks like it works.. but I got the idea which i was looking for, thanks :) :)

                W 1 Reply Last reply
                0
                • D DrukenProgrammer

                  Well I was playing with Excel sheets to database, so I was trying something on access database. and thank you for your reply, the query looks like it works.. but I got the idea which i was looking for, thanks :) :)

                  W Offline
                  W Offline
                  Wendelius
                  wrote on last edited by
                  #8

                  You're welcome :)

                  D 1 Reply Last reply
                  0
                  • W Wendelius

                    You're welcome :)

                    D Offline
                    D Offline
                    DrukenProgrammer
                    wrote on last edited by
                    #9

                    Hello Mika, I need your help on this query, you suggested me SELECT DATEDIFF(minute, start.Date_TimeWorked, end.Date_TimeWorked) AS ElapsedMinutes FROM TableName start, TableName end WHERE start.ID = end.ID AND start.Status = 'Ready' AND end.Status = 'Completed' Ready-Complete ok, but I want one more condition added to it, I need time between Date_Timeworked when status is Ready and Rejected?

                    W 1 Reply Last reply
                    0
                    • D DrukenProgrammer

                      Hello Mika, I need your help on this query, you suggested me SELECT DATEDIFF(minute, start.Date_TimeWorked, end.Date_TimeWorked) AS ElapsedMinutes FROM TableName start, TableName end WHERE start.ID = end.ID AND start.Status = 'Ready' AND end.Status = 'Completed' Ready-Complete ok, but I want one more condition added to it, I need time between Date_Timeworked when status is Ready and Rejected?

                      W Offline
                      W Offline
                      Wendelius
                      wrote on last edited by
                      #10

                      If you want them on separate rows, you can use UNION and duplicate the query (just modify Status for end condition) If you want them on the same row it would be something like (I'll change the aliases so that this becomes more clear):

                      SELECT DATEDIFF(minute, readyRow.Date_TimeWorked, completedRow.Date_TimeWorked) AS FromReadyToCompleted,
                      DATEDIFF(minute, readyRow.Date_TimeWorked, rejectedRow.Date_TimeWorked) AS FromReadyToRejected
                      FROM TableName readyRow
                      LEFT OUTER JOIN
                      TableName completedRow
                      ON readyRow.ID = completedRow.ID
                      LEFT OUTER JOIN
                      TableName rejectedRow
                      ON readyRow.ID = rejectedRow.ID
                      WHERE readyRow.Status = 'Ready'
                      AND completedRow.Status = 'Completed'
                      AND rejectedRow.Status = 'Rejected'

                      The query is now modified to outer joins since I believe that the same ID cannot be both completed and rejected at the same time. The example may contain typos, but you'll get the idea Hope this helps, Mika

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups