Results 1 to 4 of 4
  1. #1
    Accessrocks is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2015
    Posts
    2

    subquery/nested query using same table

    I am actually trying to write sql statement to generate report in ALM (quality center). It does not have capability to create multiple queries then join them together in different query (not that i know of) so I would need to do all this in one query.



    This is what I would do if I was doing it in Access.

    From table below, first I need to get ID for Parent:

    Select * from FOOD Where FATHER_ID = 0 save query as qryMain

    Then create another query:

    Select FOOD.ID, FOOD.Name, qryMain.NAME
    FROM qryMain inner join FOOD ON qrymain.ID = FOOD.ID

    Table name: FOOD
    Name
    ID
    FATHER_ID
    Breakfast
    608
    0
    Lunch
    609
    0
    Dinner
    610
    0
    Eggs
    10
    608
    bacon
    11 608
    meatloaf 22 610
    mashed potato
    23
    610
    turkey sandwich
    35
    609


    The result would something like this:

    Name Name2
    ID
    Breakfast Egg 10
    Breakfast Bacon 11
    Lunch turkey sandwich 35
    Dinner mashed potato 22
    Dinner meatloaf 23


    I know there is a way to do all this using sub/nested query but I just know basic sql so not sure where to start.

    Thank you in advance for your help

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    I don't understand why you need qryMain. All it does is select all fields and apply filter. This can be done in a query that joins the two tables directly.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Accessrocks is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2015
    Posts
    2
    Reason why I create qrymain first is because I am sql beginner and do not know how to create sub/nested queries. All the data I need is in one table.. there is only 1 table. and Correction I apologize, sql is incorrect. It should be something like this:

    Select FOOD.ID, FOOD.Name, qryMain.NAME
    FROM qryMain inner join FOOD ON qrymain.ID = FOOD.FATHER_ID



  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Okay, I see that now, same table. And the revised query does help clarify. I was just about to ask if that's what you actually intended.


    Select FOOD.ID, FOOD.Name, qryMain.NAME FROM

    (Select * from FOOD Where FATHER_ID=0) AS qryMain

    Inner join FOOD ON qryMain.ID = Food.Father_ID;
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 01-13-2014, 11:35 AM
  2. Update Query With SubQuery
    By WickidWe in forum Queries
    Replies: 7
    Last Post: 12-19-2013, 02:59 PM
  3. Replies: 11
    Last Post: 11-08-2013, 06:14 PM
  4. Query with maybe subquery
    By AndycompanyZ in forum Queries
    Replies: 4
    Last Post: 06-28-2011, 11:45 AM
  5. Replies: 1
    Last Post: 02-01-2010, 06:12 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums