Results 1 to 2 of 2
  1. #1
    skumar is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Aug 2017
    Posts
    1

    Recursive SQL Query in MS Access

    Hi Team,

    I'm new to MS Access. Please help me with the below requirement.

    I have two tables called tblParent and tblChild and I need to write a recursive query to get the final output as in below table 'Final Result'.

    I've written T-SQL recursive query using CTE but it is not working in Access because it does not support CTE. So please help me how to write recursive query to achieve the below output.



    Click image for larger version. 

Name:	Req.jpg 
Views:	28 
Size:	59.8 KB 
ID:	30050
    Join: tblParent P JOIN tbl Child C ON C.PID = P.P_Code (recursive)

    Below is my T-SQL Query:
    ;WITH CTE1 AS


    (
    SELECT P.[KeyID], 0 AS ChildKeyID, 1 AS Lvl, P.P_ID, P.R_Code, P.R_Quantity
    FROM Parent P


    UNION ALL


    SELECT T.[KeyID], C.[KeyID], T.Lvl + 1, T.P_ID, C.R_Code, C.R_Quantity
    FROM Child C
    JOIN CTE1 T ON T.R_Code = C.P_ID
    )
    ,
    CTE2 AS
    (
    SELECT [KeyID], COUNT(*) AS CNT FROM CTE1
    GROUP BY [KeyID]
    )
    SELECT T.P_ID, T.R_Code, T.R_Quantity
    FROM CTE1 T
    LEFT JOIN CTE2 C ON C.[KeyID] = T.[KeyID]
    WHERE (ChildKeyID = 0 AND Lvl = 1 AND CNT = 1) OR (ChildKeyID <> 0 AND Lvl > 1)
    ORDER BY T.[KeyID]
    OPTION (MAXRECURSION 0)

  2. #2
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    I was hoping someone who knows T-SQL would jump in and answer you. Access does not have recursive queries. First, you need to join the two tables on exact match fields and from your sample above there isn't one. In the SQL example above there are fields and values that aren't in your sample tables, so I don't know exactly what you are trying to achieve here.

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

Similar Threads

  1. One-to-one Recursive Relationship
    By kd2017 in forum Database Design
    Replies: 11
    Last Post: 08-17-2017, 05:31 PM
  2. Recursive query of companies ownerships
    By hunsnowboarder in forum Queries
    Replies: 13
    Last Post: 03-24-2016, 10:02 AM
  3. Is a recursive query possible?
    By nigelbloomy in forum Queries
    Replies: 3
    Last Post: 09-10-2015, 04:04 PM
  4. Recursive data
    By jvrsoto in forum Access
    Replies: 4
    Last Post: 12-20-2011, 10:56 AM
  5. Recursive Tables!
    By Rawb in forum Database Design
    Replies: 1
    Last Post: 01-26-2011, 02:46 PM

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