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
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Tricky Query that includes a time difference

Tricky Query that includes a time difference

Scheduled Pinned Locked Moved Database
helpdatabase
6 Posts 3 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.
  • J Offline
    J Offline
    john_berman
    wrote on last edited by
    #1

    So this query is driving me a little crazy I have a table called quadrantids2017 which has a number of events submitted by different contributors, key fields are event_id which is unique and user_ID and datetime what I want is a query that will bring back all events which ocoured on the same date within 30 seconds of each other but different contributors so here is some data event ID, user_id, datetime 37775 2 2017-01-01 00:01:23 33500 1 2017-01-01 01:07:56 37776 2 2017-01-01 01:08:45 47827 3 2017-01-01 01:09:07 37777 2 2017-01-01 01:09:44 33501 1 2017-01-01 01:14:59 47828 3 2017-01-01 01:15:00 33502 1 2017-01-01 01:16:10 37778 2 2017-01-01 01:16:47 37779 2 2017-01-01 01:19:29 37780 2 2017-01-01 01:19:36 33503 1 2017-01-01 01:24:48 So i would want a result that looks like this 33500 1 2017-01-01 01:07:56 37776 2 2017-01-01 01:07:45 33501 1 2017-01-01 01:14:59 47828 3 2017-01-01 01:15:00 37780 2 2017-01-01 01:19:36 33503 1 2017-01-01 01:19:48 On issue I have is that my current attempts bring back matches from the same contributor Help really appreciated Regards John B

    L 1 Reply Last reply
    0
    • J john_berman

      So this query is driving me a little crazy I have a table called quadrantids2017 which has a number of events submitted by different contributors, key fields are event_id which is unique and user_ID and datetime what I want is a query that will bring back all events which ocoured on the same date within 30 seconds of each other but different contributors so here is some data event ID, user_id, datetime 37775 2 2017-01-01 00:01:23 33500 1 2017-01-01 01:07:56 37776 2 2017-01-01 01:08:45 47827 3 2017-01-01 01:09:07 37777 2 2017-01-01 01:09:44 33501 1 2017-01-01 01:14:59 47828 3 2017-01-01 01:15:00 33502 1 2017-01-01 01:16:10 37778 2 2017-01-01 01:16:47 37779 2 2017-01-01 01:19:29 37780 2 2017-01-01 01:19:36 33503 1 2017-01-01 01:24:48 So i would want a result that looks like this 33500 1 2017-01-01 01:07:56 37776 2 2017-01-01 01:07:45 33501 1 2017-01-01 01:14:59 47828 3 2017-01-01 01:15:00 37780 2 2017-01-01 01:19:36 33503 1 2017-01-01 01:19:48 On issue I have is that my current attempts bring back matches from the same contributor Help really appreciated Regards John B

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Can you post your query and explain what records are returned?

      J 1 Reply Last reply
      0
      • L Lost User

        Can you post your query and explain what records are returned?

        J Offline
        J Offline
        john_berman
        wrote on last edited by
        #3

        ok my query is like this

        SELECT
        b.event_id AS a,
        b.user_ID AS a,
        b.date AS a,
        b.Time AS a
        FROM quadrantids2017 AS a
        JOIN quadrantids2017 AS b
        WHERE a.`date` = b.`date`

        AND time_to_sec(a.`Time`) - time_to_sec(b.`Time`) BETWEEN -30 AND 30

        Note in this version I am using a separate date and time field here are some of the results I have spaced and marked what should have been returned 37775 2 2017-01-01 00:01:23 33500 1 2017-01-01 01:07:56 37776 2 2017-01-01 01:08:45 37776 2 2017-01-01 01:08:45 - correct 47827 3 2017-01-01 01:09:07 - correct 47827 3 2017-01-01 01:09:07 - correct 37777 2 2017-01-01 01:09:44 - correct 33501 1 2017-01-01 01:14:59 33501 1 2017-01-01 01:14:59 47828 3 2017-01-01 01:15:00 47828 3 2017-01-01 01:15:00 33502 1 2017-01-01 01:16:10 John B

        Richard DeemingR 1 Reply Last reply
        0
        • J john_berman

          ok my query is like this

          SELECT
          b.event_id AS a,
          b.user_ID AS a,
          b.date AS a,
          b.Time AS a
          FROM quadrantids2017 AS a
          JOIN quadrantids2017 AS b
          WHERE a.`date` = b.`date`

          AND time_to_sec(a.`Time`) - time_to_sec(b.`Time`) BETWEEN -30 AND 30

          Note in this version I am using a separate date and time field here are some of the results I have spaced and marked what should have been returned 37775 2 2017-01-01 00:01:23 33500 1 2017-01-01 01:07:56 37776 2 2017-01-01 01:08:45 37776 2 2017-01-01 01:08:45 - correct 47827 3 2017-01-01 01:09:07 - correct 47827 3 2017-01-01 01:09:07 - correct 37777 2 2017-01-01 01:09:44 - correct 33501 1 2017-01-01 01:14:59 33501 1 2017-01-01 01:14:59 47828 3 2017-01-01 01:15:00 47828 3 2017-01-01 01:15:00 33502 1 2017-01-01 01:16:10 John B

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          Looks like you're missing:

          AND a.event_id != b.event_id

          from your WHERE clause. (You might want to change that to an ON clause for the JOIN.) If you want to exclude matches from the same user, you'll also need:

          AND a.user_ID != b.user_ID

          Or, to exclude matches for different users:

          AND a.user_ID = b.user_ID


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          J 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            Looks like you're missing:

            AND a.event_id != b.event_id

            from your WHERE clause. (You might want to change that to an ON clause for the JOIN.) If you want to exclude matches from the same user, you'll also need:

            AND a.user_ID != b.user_ID

            Or, to exclude matches for different users:

            AND a.user_ID = b.user_ID


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            J Offline
            J Offline
            john_berman
            wrote on last edited by
            #5

            :) so this works fine

            SELECT
            b.event_id AS a,
            b.user_ID AS a,
            b.date AS a,
            b.Time AS a
            FROM quadrantids2017 AS a
            JOIN quadrantids2017 AS b
            WHERE a.`date` = b.`date`
            AND a.event_id != b.event_id
            AND a.user_ID != b.user_ID
            AND time_to_sec(a.`Time`) - time_to_sec(b.`Time`) BETWEEN -30 AND 30

            my only query now is the length of time it took the record set its self has 3133 records and the query returned 2990 records but it took some 40 seconds to do ? - I understand its actually doing quite a lot John B

            J 1 Reply Last reply
            0
            • J john_berman

              :) so this works fine

              SELECT
              b.event_id AS a,
              b.user_ID AS a,
              b.date AS a,
              b.Time AS a
              FROM quadrantids2017 AS a
              JOIN quadrantids2017 AS b
              WHERE a.`date` = b.`date`
              AND a.event_id != b.event_id
              AND a.user_ID != b.user_ID
              AND time_to_sec(a.`Time`) - time_to_sec(b.`Time`) BETWEEN -30 AND 30

              my only query now is the length of time it took the record set its self has 3133 records and the query returned 2990 records but it took some 40 seconds to do ? - I understand its actually doing quite a lot John B

              J Offline
              J Offline
              john_berman
              wrote on last edited by
              #6

              spoke to soon, there are duplicates 37919 2 2017-01-01 11:43:02 37920 2 2017-01-01 11:43:23 47890 3 2017-01-01 11:42:56 47890 3 2017-01-01 11:42:56 47894 3 2017-01-01 12:13:31 33558 1 2017-01-01 12:13:29 47896 3 2017-01-01 13:24:44 37937 2 2017-01-01 13:24:17 37945 2 2017-01-01 14:09:53 37945 2 2017-01-01 14:09:53 sorry

              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