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. easy join question

easy join question

Scheduled Pinned Locked Moved Database
questiondatabasesaleshelpannouncement
7 Posts 4 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.
  • B Offline
    B Offline
    blakey404
    wrote on last edited by
    #1

    her is a simplified version of my problem... 2 tables... CUSTOMER customerId ... ORDER orderId customerId ... the relationship is clearly one (customer) to many (order) i want to do a join which will only bring back firt order found. so select * from customer inner join order on order.customerId = customer.customerId but in my "bigger" query this brings back all orders for each customer. so do any outer joins. IDEAS? Thanks in advance Anthony "having a thick day" Blake

    C P D 3 Replies Last reply
    0
    • B blakey404

      her is a simplified version of my problem... 2 tables... CUSTOMER customerId ... ORDER orderId customerId ... the relationship is clearly one (customer) to many (order) i want to do a join which will only bring back firt order found. so select * from customer inner join order on order.customerId = customer.customerId but in my "bigger" query this brings back all orders for each customer. so do any outer joins. IDEAS? Thanks in advance Anthony "having a thick day" Blake

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      SELECT TOP 1 ...


      Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." Ready to Give up - Your help will be much appreciated. My website

      B 1 Reply Last reply
      0
      • C Colin Angus Mackay

        SELECT TOP 1 ...


        Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." Ready to Give up - Your help will be much appreciated. My website

        B Offline
        B Offline
        blakey404
        wrote on last edited by
        #3

        just gives the first customer - i want the first order found for each customer cheers anthony

        1 Reply Last reply
        0
        • B blakey404

          her is a simplified version of my problem... 2 tables... CUSTOMER customerId ... ORDER orderId customerId ... the relationship is clearly one (customer) to many (order) i want to do a join which will only bring back firt order found. so select * from customer inner join order on order.customerId = customer.customerId but in my "bigger" query this brings back all orders for each customer. so do any outer joins. IDEAS? Thanks in advance Anthony "having a thick day" Blake

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          You could always try this:

          select * from customer
          inner join order a on a.customerId = customer.customerId
          and a.orderId = (SELECT First 1 b.orderID FROM order b WHERE b.customerID = customer.customerId)
          

          It's not going to be the most efficient SQL ever, but it should do the trick.

          Please visit http://www.readytogiveup.com/ and do something special today. Deja View - the feeling that you've seen this post before.

          B 1 Reply Last reply
          0
          • P Pete OHanlon

            You could always try this:

            select * from customer
            inner join order a on a.customerId = customer.customerId
            and a.orderId = (SELECT First 1 b.orderID FROM order b WHERE b.customerID = customer.customerId)
            

            It's not going to be the most efficient SQL ever, but it should do the trick.

            Please visit http://www.readytogiveup.com/ and do something special today. Deja View - the feeling that you've seen this post before.

            B Offline
            B Offline
            blakey404
            wrote on last edited by
            #5

            i will try something like that... i think that it will make a difference in speed but the overall query i'm working on returns 100,000+ rows so i'll have to just try and find out. cheers.

            1 Reply Last reply
            0
            • B blakey404

              her is a simplified version of my problem... 2 tables... CUSTOMER customerId ... ORDER orderId customerId ... the relationship is clearly one (customer) to many (order) i want to do a join which will only bring back firt order found. so select * from customer inner join order on order.customerId = customer.customerId but in my "bigger" query this brings back all orders for each customer. so do any outer joins. IDEAS? Thanks in advance Anthony "having a thick day" Blake

              D Offline
              D Offline
              DQNOK
              wrote on last edited by
              #6

              Rename your ORDER table to ORDERS since ORDER is a keyword (ORDER BY) that conflicts. I also took the liberty of renaming CUSTOMER to CUSTOMERS (since all table names should probably reflect a "plural" sense). The key is to form a "virtual" orders table that only contains the first order for each customer, and then join it to the CUSTOMERS INNER JOIN ORDERS table to get the other fields you want while eliminating records you don't want. Using the vendor-specific TOP keyword should work, but a more standard SQL way might be:

              SELECT * 
              FROM       (ORDERS AS O
              INNER JOIN CUSTOMERS AS C ON O.customerId = C.customerId)
              INNER JOIN (SELECT customerId, MIN(orderId) AS M
                          FROM ORDERS
                          GROUP BY customerId) AS GB ON O.orderId = GB.M;
              

              Assuming your orderId is incresing, this will give it to you. The GROUP BY is how to eliminate all the rows you don't want. HOWEVER, you'll notice that the output reproduces O.customerId, C.customerId, and GB.customerId, all of which are redundant. The only way I know to preventing listing them all is to spell-out the fields you want instead of using '*'. Hope this helps. David

              B 1 Reply Last reply
              0
              • D DQNOK

                Rename your ORDER table to ORDERS since ORDER is a keyword (ORDER BY) that conflicts. I also took the liberty of renaming CUSTOMER to CUSTOMERS (since all table names should probably reflect a "plural" sense). The key is to form a "virtual" orders table that only contains the first order for each customer, and then join it to the CUSTOMERS INNER JOIN ORDERS table to get the other fields you want while eliminating records you don't want. Using the vendor-specific TOP keyword should work, but a more standard SQL way might be:

                SELECT * 
                FROM       (ORDERS AS O
                INNER JOIN CUSTOMERS AS C ON O.customerId = C.customerId)
                INNER JOIN (SELECT customerId, MIN(orderId) AS M
                            FROM ORDERS
                            GROUP BY customerId) AS GB ON O.orderId = GB.M;
                

                Assuming your orderId is incresing, this will give it to you. The GROUP BY is how to eliminate all the rows you don't want. HOWEVER, you'll notice that the output reproduces O.customerId, C.customerId, and GB.customerId, all of which are redundant. The only way I know to preventing listing them all is to spell-out the fields you want instead of using '*'. Hope this helps. David

                B Offline
                B Offline
                blakey404
                wrote on last edited by
                #7

                Thats great. Don't worry about the table names - they were hyperthetical - trust me postng the actual query wouldnt have provoked any response it's huge.

                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