STRING_AGG with distinct values

I'm currently using STRING_AGG to display a list of barcodes per SKU. Unfortunately, our database includes duplicate barcodes, and the result displays all of them. For example, "1234, 5678, 1234, 5678."

I understand that I can use SELECT DISTINCT to only display unique barcode values, but I'm not sure how to implement DISTINCT in only one column. Is there a way to resolve this or a better way to go about this issue?

Our current code, simplified, looks as below:

SELECT StoreCode,
	SKU,
	STRING_AGG(Barcode, ', '),
FROM ItemSummaryView
	WHERE SKU = @SKU
GROUP BY StoreCode, SKU
ORDER BY StoreCode, SKU

Leave a Comment