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. How to identify identical master-detail records.

How to identify identical master-detail records.

Scheduled Pinned Locked Moved Database
databasehelptutorialquestion
7 Posts 5 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
    David Mujica
    wrote on last edited by
    #1

    Without going into gruesome detail, I would like to get some ideas on how I might approach this problem. Given that I have a classic master-detail relationship in my database, lets call the tables Master and Detail. There is a master ID, and a detail ID for every row and every detail record has a reference to the master ID which it belongs. If master record A has 3 detail lines (Part 1, Part 2, Part 3) and master record B also has those same detail records (Part 1, Part 2, Part 3) then I want to indicate that master record A and B are identical. Can anyone suggest a method to do this ? My initial idea is to use 2 SQL cursors where I would be looping through the data with Cursor1 from top to bottom, then use Cursor2 to scan the same data for each occurence of Cursor1. Jeeze, sounds kind of lame. X| There must be a better way. Any suggestions would be greatly appreciated.

    Richard DeemingR L G 3 Replies Last reply
    0
    • D David Mujica

      Without going into gruesome detail, I would like to get some ideas on how I might approach this problem. Given that I have a classic master-detail relationship in my database, lets call the tables Master and Detail. There is a master ID, and a detail ID for every row and every detail record has a reference to the master ID which it belongs. If master record A has 3 detail lines (Part 1, Part 2, Part 3) and master record B also has those same detail records (Part 1, Part 2, Part 3) then I want to indicate that master record A and B are identical. Can anyone suggest a method to do this ? My initial idea is to use 2 SQL cursors where I would be looping through the data with Cursor1 from top to bottom, then use Cursor2 to scan the same data for each occurence of Cursor1. Jeeze, sounds kind of lame. X| There must be a better way. Any suggestions would be greatly appreciated.

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

      Something along these lines might get you started:

      SELECT
      A.ID As OriginalID,
      B.ID As DuplicateID
      FROM
      -- Every pair of master records:
      MasterTable As A
      CROSS JOIN
      MasterTable As B
      WHERE
      -- Only those pairs where A is earlier than B:
      A.ID < B.ID
      And
      -- All detail lines for A exist for B:
      Not Exists
      (
      SELECT
      (relevant columns)
      FROM
      DetailTable
      WHERE
      MasterID = A.ID

        EXCEPT
        
        SELECT
           (relevant columns)
        FROM
           DetailTable
        WHERE
           MasterID = B.ID
      

      )
      And
      -- All detail lines for B exist for A:
      Not Exists
      (
      SELECT
      (relevant columns)
      FROM
      DetailTable
      WHERE
      MasterID = B.ID

        EXCEPT
        
        SELECT
           (relevant columns)
        FROM
           DetailTable
        WHERE
           MasterID = A.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

      D 1 Reply Last reply
      0
      • D David Mujica

        Without going into gruesome detail, I would like to get some ideas on how I might approach this problem. Given that I have a classic master-detail relationship in my database, lets call the tables Master and Detail. There is a master ID, and a detail ID for every row and every detail record has a reference to the master ID which it belongs. If master record A has 3 detail lines (Part 1, Part 2, Part 3) and master record B also has those same detail records (Part 1, Part 2, Part 3) then I want to indicate that master record A and B are identical. Can anyone suggest a method to do this ? My initial idea is to use 2 SQL cursors where I would be looping through the data with Cursor1 from top to bottom, then use Cursor2 to scan the same data for each occurence of Cursor1. Jeeze, sounds kind of lame. X| There must be a better way. Any suggestions would be greatly appreciated.

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

        You can use the GROUP BY clause on the Detail table with Part1, 2 and 3 in the group clause and take the distinct DetailID and MasterID from the table. But Group By requires an aggregate function and I'm not sure how to take distinct values using it. If you are sure you need only 2 matching rows, you can use MIN and MAX functions of course.

        1 Reply Last reply
        0
        • D David Mujica

          Without going into gruesome detail, I would like to get some ideas on how I might approach this problem. Given that I have a classic master-detail relationship in my database, lets call the tables Master and Detail. There is a master ID, and a detail ID for every row and every detail record has a reference to the master ID which it belongs. If master record A has 3 detail lines (Part 1, Part 2, Part 3) and master record B also has those same detail records (Part 1, Part 2, Part 3) then I want to indicate that master record A and B are identical. Can anyone suggest a method to do this ? My initial idea is to use 2 SQL cursors where I would be looping through the data with Cursor1 from top to bottom, then use Cursor2 to scan the same data for each occurence of Cursor1. Jeeze, sounds kind of lame. X| There must be a better way. Any suggestions would be greatly appreciated.

          G Offline
          G Offline
          GuyThiebaut
          wrote on last edited by
          #4

          You could use something like the except or minus(depending on your flavour of sql) clause to find differences between queries for rows referencing the same master ID. This would return differences, those rows not returned would be equal.

          “That which can be asserted without evidence, can be dismissed without evidence.”

          ― Christopher Hitchens

          1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            Something along these lines might get you started:

            SELECT
            A.ID As OriginalID,
            B.ID As DuplicateID
            FROM
            -- Every pair of master records:
            MasterTable As A
            CROSS JOIN
            MasterTable As B
            WHERE
            -- Only those pairs where A is earlier than B:
            A.ID < B.ID
            And
            -- All detail lines for A exist for B:
            Not Exists
            (
            SELECT
            (relevant columns)
            FROM
            DetailTable
            WHERE
            MasterID = A.ID

              EXCEPT
              
              SELECT
                 (relevant columns)
              FROM
                 DetailTable
              WHERE
                 MasterID = B.ID
            

            )
            And
            -- All detail lines for B exist for A:
            Not Exists
            (
            SELECT
            (relevant columns)
            FROM
            DetailTable
            WHERE
            MasterID = B.ID

              EXCEPT
              
              SELECT
                 (relevant columns)
              FROM
                 DetailTable
              WHERE
                 MasterID = A.ID
            

            )


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

            D Offline
            D Offline
            David Mujica
            wrote on last edited by
            #5

            Wow, that looks very interesting. I will see if I can get it to work.

            M 1 Reply Last reply
            0
            • D David Mujica

              Wow, that looks very interesting. I will see if I can get it to work.

              M Offline
              M Offline
              Mycroft Holmes
              wrote on last edited by
              #6

              Normal way to acknowledge thanks is to up vote the answer - a message is appreciated as well.

              Never underestimate the power of human stupidity RAH

              Richard DeemingR 1 Reply Last reply
              0
              • M Mycroft Holmes

                Normal way to acknowledge thanks is to up vote the answer - a message is appreciated as well.

                Never underestimate the power of human stupidity RAH

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

                An up-vote before you know whether the answer works might be a bit premature.


                "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

                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