Solved Concatenating into nested records (1 Viewer)

niawo

New member
Local time
Today, 16:40
Joined
Jul 5, 2017
Messages
14
Hi. I'm trying to concatenate records from different fields in a query, and found this old database by Duane Hookom, which I've adapted slightly. However, I'm trying to get it to nest the concatenated results. e.g. in queNames I'm trying to get Christina (Associate, Neighbor, Partner), rather than listed separately, but can't work out how to do that. Is there a straightforward way to do it?

Thanks!
 

Attachments

  • Concat2k.mdb
    508 KB · Views: 72

niawo

New member
Local time
Today, 16:40
Joined
Jul 5, 2017
Messages
14
Hi. Thanks for this. However, it seems it will only list Christina (Associate), Christina (Neighbor), Christina (Partner), rather than nesting them as above. Or am I doing something wrong?
 

Josef P.

Well-known member
Local time
Today, 17:40
Joined
Feb 2, 2023
Messages
826
Not necessarily pretty, but works with the existing function:
Code:
SELECT
   tblFamily.FamID, tblFamily.famLastName,
   Concatenate("SELECT distinct FirstName & ' (' &
                     Concatenate(""select Role from tblFamMem where FamID="" & [FamID] & "" and  FirstName ='"" & FirstName & ""'"") & ')'
              FROM tblFamMem WHERE FamID =" & [FamID]) AS FirstNames
FROM tblFamily;

BTW: It can also happen that father and son have the same first name. => Simon (Dad, Son)
 

cheekybuddha

AWF VIP
Local time
Today, 16:40
Joined
Jul 21, 2014
Messages
2,280
The real problem is in the table design.

tblFamMem should not contain the role. Role should be linked to a family member through a M:M link (junction table and Roles table)

In your tables as you have it Christina is four different people.
 

June7

AWF VIP
Local time
Today, 07:40
Joined
Mar 9, 2014
Messages
5,472
Have to use FamID along with FirstName as member identifier. Unless kids have same name ala George Foreman, also as Josef stated, even that combination won't handle same name in multiple generations.
 

niawo

New member
Local time
Today, 16:40
Joined
Jul 5, 2017
Messages
14
Not necessarily pretty, but works with the existing function:
Code:
SELECT
   tblFamily.FamID, tblFamily.famLastName,
   Concatenate("SELECT distinct FirstName & ' (' &
                     Concatenate(""select Role from tblFamMem where FamID="" & [FamID] & "" and  FirstName ='"" & FirstName & ""'"") & ')'
              FROM tblFamMem WHERE FamID =" & [FamID]) AS FirstNames
FROM tblFamily;

BTW: It can also happen that father and son have the same first name. => Simon (Dad, Son)
Thanks! This works perfectly!
 

niawo

New member
Local time
Today, 16:40
Joined
Jul 5, 2017
Messages
14
Not necessarily pretty, but works with the existing function:
Code:
SELECT
   tblFamily.FamID, tblFamily.famLastName,
   Concatenate("SELECT distinct FirstName & ' (' &
                     Concatenate(""select Role from tblFamMem where FamID="" & [FamID] & "" and  FirstName ='"" & FirstName & ""'"") & ')'
              FROM tblFamMem WHERE FamID =" & [FamID]) AS FirstNames
FROM tblFamily;

BTW: It can also happen that father and son have the same first name. => Simon (Dad, Son)
Sorry, do you know if it's possible to add ORDER BY to this?
 

cheekybuddha

AWF VIP
Local time
Today, 16:40
Joined
Jul 21, 2014
Messages
2,280
What are you trying to ORDER ?

By FirstName, or by Role within the concatenated list?
 

Josef P.

Well-known member
Local time
Today, 17:40
Joined
Feb 2, 2023
Messages
826
It's by an additional field in tblFamMem
Which one?

Distinct + Order by: the data field must be present in the select part.
You could also use a group-by statement instead of distinct.
 

niawo

New member
Local time
Today, 16:40
Joined
Jul 5, 2017
Messages
14
Which one?

Distinct + Order by: the data field must be present in the select part.
You could also use a group-by statement instead of distinct.
Thanks. It's the Order field here in tblFamMem
 

Attachments

  • Concat2k.mdb
    560 KB · Views: 54

Josef P.

Well-known member
Local time
Today, 17:40
Joined
Feb 2, 2023
Messages
826
Is that what you mean?
Code:
SELECT
   FamID, famLastName
   , Concatenate("
                 SELECT distinct
                             FirstName & ' (' & Concatenate(
                                   ""select Role from tblFamMem where FamID="" & [FamID] & "" and  FirstName ='"" & FirstName & ""'"") & ')'
                             , [Order]
                   FROM
                          tblFamMem WHERE FamID =" & [FamID] & " order by [Order]"
     ) AS FirstNames
FROM
    tblFamily

BTW: It would be nice to get a sample file that you can open without errors. Shows the value a little bit for me.
 
Last edited:

niawo

New member
Local time
Today, 16:40
Joined
Jul 5, 2017
Messages
14
Is that what you mean?
Code:
SELECT
   FamID, famLastName
   , Concatenate("
                 SELECT distinct
                             FirstName & ' (' & Concatenate(
                                   ""select Role from tblFamMem where FamID="" & [FamID] & "" and  FirstName ='"" & FirstName & ""'"") & ')'
                             , [Order]
                   FROM
                          tblFamMem WHERE FamID =" & [FamID] & " order by [Order]"
     ) AS FirstNames
FROM
    tblFamily

BTW: It would be nice to get a sample file that you can open without errors. Shows the value a little bit for me.

Yes, that works great. Many thanks! And apologies for the errors.
 

Users who are viewing this thread

Top Bottom