Results 1 to 5 of 5

Thread: Nested SQL Query

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Nested SQL Query

    I have a select statement to pull PICKSL, SLOT, PROD, DESC, PACK, MANU ID, HITIX, and PALLET QTY from the (PICKSL) table. The present nested select statement grabs all the records whose product numbers are NOT already in the ALTER table.

    PROBLEM: I would like to pull the SLOT field from the TEST table since the PICKSL table does not have a SLOT field. Is there a way to do this in SQL? Thank you very much in advance.

    VB Code:
    1. sql = "INSERT INTO MAIN ( PICKSL, SLOT, PROD, [DESC], PACK, " & _
    2.           "[MANU ID], HITIX, [PALLET QTY] ) " & _
    3.           "SELECT p.PICKSL, p.PICKSL, p.PROD, p.DESC, p.PACK, " & _
    4.           "p.MANUID, p.HITI, p.OH " & _
    5.           "FROM PICKSL AS p " & _
    6.           "WHERE Not Exists ( " & _
    7.             "SELECT * " & _
    8.             "FROM [ALTER] a " & _
    9.             "WHERE  a.PROD = p.PROD);"

    Nenio foriras ĝis ĝi havas instru ni kiu ni devas scii.

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Nested SQL Query

    Quote Originally Posted by manofsteel00
    PROBLEM: I would like to pull the SLOT field from the TEST table since the PICKSL table does not have a SLOT field. Is there a way to do this in SQL?
    If there's a link field between the Test table and the PickSL table you could use a Join.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Nested SQL Query

    Nested queries are expensive and if you have some sort of a primary key that both tables 'know', you should use a JOIN. Also, why are you PICKing PICKSL twice? ( )

    Code:
    SELECT p.Picksl, p.Prod, p.Desc, p.Pack, p.Manuid, p.hiti, p.oh, t.Slot FROM
    PICKSL P LEFT JOIN Alter A ON A.Prod = B.Prod 
    LEFT JOIN Test T ON P.SomeID = T.SomeID
    WHERE B.Prod IS NULL

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Re: Nested SQL Query

    The PICKSL table does not have the SLOT field; it has everything but the slot field. I would like to create a query that pulls the SLOT field from the TEST table and the rest of the information from the PICKSL table to populate a listbox. My current query pulls all the information correctly except for the SLOT field, which is why I am currently just using the pick slot for the slot field.

    Nenio foriras ĝis ĝi havas instru ni kiu ni devas scii.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Exclamation Re: Nested SQL Query

    The following code actually gives me the appropriate information EXCEPT if a product is not listed in the TEST table it will not be inserted to the MAIN table. In other words, there are some items that do not appear on the TEST table; therefore, their SLOT should be the same as their PICKSL. Would it just be better to loop through the database and set the SLOTs appropriately or is there a way I can retrieve all the records from the PICKSL table even though their SLOT is not on the TEST table? Please feel free to post any questions for clarity. Thank you in advance.

    VB Code:
    1. INSERT INTO MAIN ( PICKSL, SLOT, PROD, [DESC], PACK, [MANU ID], HITIX, [PALLET QTY] )
    2. SELECT p.PICKSL, t.SLOT, p.PROD, p.DESC, p.PACK, p.MANUID, p.HITI, p.OH
    3. FROM PICKSL AS p, TEST AS t
    4. WHERE Not Exists (
    5.             SELECT *
    6.             FROM [ALTER] a
    7.             WHERE  a.PROD = p.PROD)
    8.      AND t.PROD = p.PROD;

    Nenio foriras ĝis ĝi havas instru ni kiu ni devas scii.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width