I have scenario where i want the invoice numbers from two tables in csv list. I could accomplish this using Stuff as below:
SELECT
sopnumbe
,custname
,Invoices =
STUFF((SELECT DISTINCT ', '+ RTRIM(a.sopnumbe)+', '+ RTRIM(B.sopnumbe)
FROM
SOP10100 a
fULL OUTER JOIN
SOP30200 B
ON a.ORIGNUMB =B.ORIGNUMB
WHERE a.ORIGNUMB =@ordernumb
FOR XML PATH('')) , 1 , 1 , '' )
FROM SOP10100
WHERE
SOPNUMBE = @ordernumb
The above query produces right results except for the repetitive invoice numbers in the list. Is there any other way to accomplish this?
if the result of simple outer join query is:
INV1527157 INV1523836
INV1527157 INV1526475
the result of stuff query:
K08081383206 BACHELOR CONTROLS INC INV1527157, INV1523836, INV1527157, INV1526475
INV1527157 gets repeated, i want to get rid of repetition.
You may want to try
UNIONthe results together fromAandB, rather than a full outer join:This will return only the distinct results from both subqueries.