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. JOIN Problem

JOIN Problem

Scheduled Pinned Locked Moved Database
helpdatabasequestion
12 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.
  • K Kevin Marois

    SQL 2012: I have two tables, AttemptedRuns and Bays This

    SELECT *
    FROM NexGen.dbo.AttemptedRuns
    WHERE BaySerialNumber = '1545300531'

    Produces 317 rows. The table has a column called 'BaySerialNumber' So I want to join on bays so I can retrieve the BayLocation:

    SELECT ar.Id as AttemptedRunId,
    ar.AccessionId,
    ar.StartTime,
    ar.BaySerialNumber,
    ar.CartridgeId,
    ar.Result as AttemptedRunResult,
    b.Location as BayLocation
    FROM NexGen.dbo.AttemptedRuns ar
    LEFT JOIN NexGen.dbo.Bays b on b.SerialNumber = ar.BaySerialNumber
    WHERE ar.BaySerialNumber = '1545300531'

    The problem is that I now get 1902 rows. It's duplicating the AttemptedRuns rows. [See here](https://1drv.ms/i/s!AlkRTpT49yCMgiGkgyxj747ZTe1d) What I am looking for is to get the BayLocation from the Bays table for each AttemptedRow row. What am I doing wrong??

    If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

    Z Offline
    Z Offline
    ZurdoDev
    wrote on last edited by
    #2

    There are more matches than 1 to 1. If you change it to SELECT * you'll see all the fields in both tables and can then see why you are getting more than one match.

    There are two kinds of people in the world: those who can extrapolate from incomplete data. There are only 10 types of people in the world, those who understand binary and those who don't.

    K 1 Reply Last reply
    0
    • Z ZurdoDev

      There are more matches than 1 to 1. If you change it to SELECT * you'll see all the fields in both tables and can then see why you are getting more than one match.

      There are two kinds of people in the world: those who can extrapolate from incomplete data. There are only 10 types of people in the world, those who understand binary and those who don't.

      K Offline
      K Offline
      Kevin Marois
      wrote on last edited by
      #3

      OK, but what I really want is to just get the BayLocation for each AttemptedRun record. Is this possible?

      If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

      Z L 2 Replies Last reply
      0
      • K Kevin Marois

        OK, but what I really want is to just get the BayLocation for each AttemptedRun record. Is this possible?

        If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

        Z Offline
        Z Offline
        ZurdoDev
        wrote on last edited by
        #4

        Kevin Marois wrote:

        the BayLocation for each AttemptedRun record.

        I believe that is what you have originally:

        SELECT ar.Id as AttemptedRunId,
        ar.AccessionId,
        ar.StartTime,
        ar.BaySerialNumber,
        ar.CartridgeId,
        ar.Result as AttemptedRunResult,
        b.Location as BayLocation
        FROM AttemptedRuns ar
        LEFT JOIN Bays b on b.SerialNumber = ar.BaySerialNumber
        WHERE ar.BaySerialNumber = '1545300531'

        This says to get all attempted runs and if there is a bay that matches on BaySerialNumber to get its information as well.

        There are two kinds of people in the world: those who can extrapolate from incomplete data. There are only 10 types of people in the world, those who understand binary and those who don't.

        K 1 Reply Last reply
        0
        • Z ZurdoDev

          Kevin Marois wrote:

          the BayLocation for each AttemptedRun record.

          I believe that is what you have originally:

          SELECT ar.Id as AttemptedRunId,
          ar.AccessionId,
          ar.StartTime,
          ar.BaySerialNumber,
          ar.CartridgeId,
          ar.Result as AttemptedRunResult,
          b.Location as BayLocation
          FROM AttemptedRuns ar
          LEFT JOIN Bays b on b.SerialNumber = ar.BaySerialNumber
          WHERE ar.BaySerialNumber = '1545300531'

          This says to get all attempted runs and if there is a bay that matches on BaySerialNumber to get its information as well.

          There are two kinds of people in the world: those who can extrapolate from incomplete data. There are only 10 types of people in the world, those who understand binary and those who don't.

          K Offline
          K Offline
          Kevin Marois
          wrote on last edited by
          #5

          RyanDev wrote:

          This says to get all attempted runs and if there is a bay that matches on BaySerialNumber to get its information as well.

          Correct, What I'm looking for is the APPEND the BayLocation to the end of each row of AttemptedRun data

          If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

          Z 1 Reply Last reply
          0
          • K Kevin Marois

            RyanDev wrote:

            This says to get all attempted runs and if there is a bay that matches on BaySerialNumber to get its information as well.

            Correct, What I'm looking for is the APPEND the BayLocation to the end of each row of AttemptedRun data

            If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

            Z Offline
            Z Offline
            ZurdoDev
            wrote on last edited by
            #6

            But you have 6 matching rows in Bays. BaySerialNumber is not unique. That is why you need to run SELECT * and see why you are getting extra. Perhaps there is another field, like status, that you need to take into account. We can't know because we can't see all the data.

            There are two kinds of people in the world: those who can extrapolate from incomplete data. There are only 10 types of people in the world, those who understand binary and those who don't.

            K 1 Reply Last reply
            0
            • K Kevin Marois

              OK, but what I really want is to just get the BayLocation for each AttemptedRun record. Is this possible?

              If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

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

              Unless I mistake what you're asking, that should be doable using a correlated subquery. Something along the lines of

              SELECT *, -- 'everything from the attemptedrun record
              (SELECT BayLocation FROM NexGen.dbo.Bays AS bl WHERE bl.SerialNumber = ar.BaySerialNumber) AS [MyNewColumn]
              FROM NexGen.dbo.AttemptedRun AS ar
              WHERE BaySerialNumber = '1545300531'

              That is assuming that each bay is uniquely numbered on said field.

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

              Z 1 Reply Last reply
              0
              • Z ZurdoDev

                But you have 6 matching rows in Bays. BaySerialNumber is not unique. That is why you need to run SELECT * and see why you are getting extra. Perhaps there is another field, like status, that you need to take into account. We can't know because we can't see all the data.

                There are two kinds of people in the world: those who can extrapolate from incomplete data. There are only 10 types of people in the world, those who understand binary and those who don't.

                K Offline
                K Offline
                Kevin Marois
                wrote on last edited by
                #8

                Well you are right. Turns out there are 6 rows in Bays with the same serial number. I was operating under the assumption that the serial number was unique. Thanks

                If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

                Z 1 Reply Last reply
                0
                • K Kevin Marois

                  Well you are right. Turns out there are 6 rows in Bays with the same serial number. I was operating under the assumption that the serial number was unique. Thanks

                  If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

                  Z Offline
                  Z Offline
                  ZurdoDev
                  wrote on last edited by
                  #9

                  No problem. You may find that you need to use a different column to ensure uniqueness. Or there may be another table altogether. Who knows. Good luck.

                  There are two kinds of people in the world: those who can extrapolate from incomplete data. There are only 10 types of people in the world, those who understand binary and those who don't.

                  K 1 Reply Last reply
                  0
                  • Z ZurdoDev

                    No problem. You may find that you need to use a different column to ensure uniqueness. Or there may be another table altogether. Who knows. Good luck.

                    There are two kinds of people in the world: those who can extrapolate from incomplete data. There are only 10 types of people in the world, those who understand binary and those who don't.

                    K Offline
                    K Offline
                    Kevin Marois
                    wrote on last edited by
                    #10

                    Ya I learned that the hard way ;P First, I haven't done SQL in a while, so it's a bit blurry. Second, I don't really know the data too well. it's all fun & games now ;P

                    If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

                    1 Reply Last reply
                    0
                    • L Lost User

                      Unless I mistake what you're asking, that should be doable using a correlated subquery. Something along the lines of

                      SELECT *, -- 'everything from the attemptedrun record
                      (SELECT BayLocation FROM NexGen.dbo.Bays AS bl WHERE bl.SerialNumber = ar.BaySerialNumber) AS [MyNewColumn]
                      FROM NexGen.dbo.AttemptedRun AS ar
                      WHERE BaySerialNumber = '1545300531'

                      That is assuming that each bay is uniquely numbered on said field.

                      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

                      Z Offline
                      Z Offline
                      ZurdoDev
                      wrote on last edited by
                      #11

                      If you look at the data in his screenshot you'll see that there are different values for BayLocation per run so this approach would give an error about multiple records in a subquery.

                      There are two kinds of people in the world: those who can extrapolate from incomplete data. There are only 10 types of people in the world, those who understand binary and those who don't.

                      L 1 Reply Last reply
                      0
                      • Z ZurdoDev

                        If you look at the data in his screenshot you'll see that there are different values for BayLocation per run so this approach would give an error about multiple records in a subquery.

                        There are two kinds of people in the world: those who can extrapolate from incomplete data. There are only 10 types of people in the world, those who understand binary and those who don't.

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

                        I didn't look; he said he wanted the entire record, and add a single column. Means it is also easy to debug the subquery by using a value from the database as example. There will probably be multiple records with the same ar.BaySerialNumber, and I'm hoping that b.SerialNumber is the primary key (or at least uniquely indexed) on the Bays-table. Even if there's a one to one relation, that should work. So, what the schema of the tables? Would be nice to have a CREATE script for both tables :) --edit Asking the wrong person, and was already solved.

                        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

                        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