Results 1 to 7 of 7
  1. #1
    fkotulak is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2016
    Posts
    3

    merging a one to many relationship into single record results using one sql statement

    Here's my problem. I'm trying to get row results where these two tables are combined. I should be able to be do this with joins and unions I just can't seem to get it to work.




    table1
    recnum
    last name


    table2
    recnum
    childname
    ordinal

    1 to many relationship


    sample


    Table1
    1, Jones
    2, Smith


    Table2
    1,Mary,1
    1,John,2
    1,Petunia,3
    2,Louise,1
    2,Sal,2


    I need a single SQL command that will give me row results like


    Jones, Mary, John, Petunia
    Smith, Louise, Sal

    Any help appreciated!

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    See if this works for you:

    http://allenbrowne.com/func-concat.html
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    fkotulak is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2016
    Posts
    3

    Thanks but I'm trying to avoid VBA and that's a lot of code

    I actually solved this some time ago when I was actively doing database programming. But I forgot and I didn't save the solution. I know it can be done in one SQL select command. I remember it took me awhile to figure it out!

    If I get no other single command select responses I will try and implement your solution which looks like it should work and it is what I'm trying to do!


    Quote Originally Posted by pbaldy View Post
    See if this works for you:

    http://allenbrowne.com/func-concat.html

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    It could be done with subqueries, but that wouldn't be as dynamic. I'll be interested to see the method if it exists.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    InsuranceGuy is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Aug 2015
    Location
    Colorado
    Posts
    126
    What you're attempting to do is not possible in MS Access without using VBA (AFAIK). In SQL Server and Oracle there are ways to do this using aggregate concatenating functions, but those do not exist in MS Access.

    A VBA function could take your value from Table 1 and build the concatenating values from table 2 though, but that requires a query and a VBA function--doesn't sound like what you wanted to do.


    Jeff

  6. #6
    fkotulak is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2016
    Posts
    3

    Got the solution but was not easy. Friggin Access is very touchy

    SELECT t1.*,
    t2.Child as Child1, t2.birthdate as Bday1,
    t3.Child as Child2, t3.birthdate as Bday2,
    t4.Child as Child3, t4.birthdate as Bday3,
    t5.Child as Child4, t5.birthdate as Bday4
    FROM (((direct AS t1
    LEFT JOIN children AS t2 ON (t1.[record number] = t2.[record number] and t2.Ordinal = 1))
    LEFT JOIN children AS t3 ON (t1.[record number] = t3.[record number] and t3.Ordinal = 2))
    LEFT JOIN children AS t4 ON (t1.[record number] = t4.[record number] and t4.Ordinal = 3))
    LEFT JOIN children AS t5 ON (t1.[record number] = t5.[record number] and t5.Ordinal = 4)
    WHERE ((t1.Business)=No)
    ORDER BY t1.[last name], t1.[First Name]
    WITH OWNERACCESS OPTION;

    This is fairly standard SQL but the problem came down to Access required parens! This will not work if you do not use the parens. I found out that multiple joins in Access require parens around each. Also you will get "Joins are not supported errors" unless you paren the ON statements when you have a value test?? Huh! This is an apparent "bug" in Access that has not been corrected in 2010. Another Access "bug" occurs after you've run the above type query and it works and then you decide to modify it some more. Access takes out all the ON statement parens. So you have to put them back in again every time. I guess this is the difference between a pseudo-database and a real database like SQL Server.









    Quote Originally Posted by InsuranceGuy View Post
    What you're attempting to do is not possible in MS Access without using VBA (AFAIK). In SQL Server and Oracle there are ways to do this using aggregate concatenating functions, but those do not exist in MS Access.

    A VBA function could take your value from Table 1 and build the concatenating values from table 2 though, but that requires a query and a VBA function--doesn't sound like what you wanted to do.


    Jeff

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Like I said, not dynamic, plus it requires the ordinal field which in many/most situations won't exist.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 1
    Last Post: 11-25-2016, 11:57 PM
  2. Replies: 2
    Last Post: 11-22-2014, 07:50 AM
  3. Replies: 2
    Last Post: 09-10-2014, 08:48 AM
  4. Replies: 5
    Last Post: 05-10-2014, 10:05 AM
  5. Replies: 1
    Last Post: 08-24-2012, 08:24 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