Hi
I have a table with field BPLId. I want to query another table which have same BPLId and then concatenate LocCode field from another table
which have same DocNum of that same BPLId records using Sql Query.
Thanks
Hi
I have a table with field BPLId. I want to query another table which have same BPLId and then concatenate LocCode field from another table
which have same DocNum of that same BPLId records using Sql Query.
Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sangeetha SRank #212 · 11.4K reputationPosted Apr 1, 2025, 12:24 AM EDT
Let's consider you have two tables:
Table1with fieldsBPLIdandDocNumTable2with fieldsBPLId,DocNum, andLocCodeYou want to concatenate the
LocCodefield fromTable2for records with the sameBPLIdandDocNum.Here's how you can do it:
SELECT t1.BPLId, t1.DocNum, STRING_AGG(t2.LocCode, ', ') AS ConcatenatedLocCodes FROM Table1 t1 JOIN Table2 t2 ON t1.BPLId = t2.BPLId AND t1.DocNum = t2.DocNum GROUP BY t1.BPLId, t1.DocNum;Explanation:
Table1andTable2whereBPLIdandDocNummatch.LocCodevalues fromTable2into a single string, separated by commas.BPLIdandDocNum.If you are using MySQL, you can use
GROUP_CONCATinstead:SELECT t1.BPLId, t1.DocNum, GROUP_CONCAT(t2.LocCode SEPARATOR ', ') AS ConcatenatedLocCodes FROM Table1 t1 JOIN Table2 t2 ON t1.BPLId = t2.BPLId AND t1.DocNum = t2.DocNum GROUP BY t1.BPLId, t1.DocNum;Tuhin PaulRank #7 · 71.9K reputationPosted Apr 1, 2025, 2:36 AM EDT
To achieve this, you can use a combination of SQL
JOINand string aggregation functions (likeGROUP_CONCATin MySQL orSTRING_AGGin PostgreSQL/SQL Server).BPLId.DocNumand concatenate theLocCodevalues for each group.In MySQL, you can use the
GROUP_CONCATfunction to concatenate values.SELECT t1.BPLId, t1.DocNum, GROUP_CONCAT(t2.LocCode ORDER BY t2.LocCode SEPARATOR ', ') AS ConcatenatedLocCodes FROM Table1 t1 JOIN Table2 t2 ON t1.BPLId = t2.BPLId GROUP BY t1.BPLId, t1.DocNum;t1represents the first table (Table1) withBPLIdandDocNum.t2represents the second table (Table2) withBPLIdandLocCode.JOINensures that only rows with matchingBPLIdin both tables are considered.GROUP_CONCATconcatenates theLocCodevalues for eachDocNumgroup.ORDER BY t2.LocCodeensures the concatenated values are sorted alphabetically.SEPARATOR ', 'specifies the delimiter between concatenated values.Tuhin PaulRank #7 · 71.9K reputationPosted Apr 1, 2025, 2:37 AM EDT
In PostgreSQL, you can use the
STRING_AGGfunction.SELECT t1.BPLId, t1.DocNum, STRING_AGG(t2.LocCode, ', ' ORDER BY t2.LocCode) AS ConcatenatedLocCodes FROM Table1 t1 JOIN Table2 t2 ON t1.BPLId = t2.BPLId GROUP BY t1.BPLId, t1.DocNum;STRING_AGGis used to concatenateLocCodevalues with a specified delimiter (', ').ORDER BYclause insideSTRING_AGGensures the concatenated values are sorted.In SQL Server, you can use the
STRING_AGGfunction (introduced in SQL Server 2017) orFOR XML PATHfor older versions.Using
STRING_AGG(SQL Server 2017+):SELECT t1.BPLId, t1.DocNum, STRING_AGG(t2.LocCode, ', ') WITHIN GROUP (ORDER BY t2.LocCode) AS ConcatenatedLocCodes FROM Table1 t1 JOIN Table2 t2 ON t1.BPLId = t2.BPLId GROUP BY t1.BPLId, t1.DocNum;Using
FOR XML PATH(Older Versions):SELECT t1.BPLId, t1.DocNum, STUFF(( SELECT ', ' + t2.LocCode FROM Table2 t2 WHERE t1.BPLId = t2.BPLId FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 2, '') AS ConcatenatedLocCodes FROM Table1 t1 GROUP BY t1.BPLId, t1.DocNum;STUFFremoves the leading delimiter (,) from the concatenated string.FOR XML PATH('')is used to concatenate values into a single string.Tuhin PaulRank #7 · 71.9K reputationPosted Apr 1, 2025, 2:41 AM EDT
Example Data
Table1 (
Table1):BPLId
DocNum
1
101
1
102
2
103
Table2 (
Table2):BPLId
LocCode
1
A
1
B
1
C
2
X
2
Y
Result:
BPLId
DocNum
ConcatenatedLocCodes
1
101
A, B, C
1
102
A, B, C
2
103
X, Y