I have below table structure
create TABLE PRODUCTDeatils
(
Product varchar(50),
Date Datetime
)
I want an output where i get comma separated list with distinct values but order by date.
I first split the values by below query
SELECT DISTINCT
LTRIM(RTRIM(value)) AS Product, date
INTO #tmp3
FROM PRODUCTDeatils
CROSS APPLY STRING_SPLIT(LTRIM(RTRIM(Product)), ',')
SELECT *
FROM #tmp3
ORDER BY date
Then I used below two queries to achieve it but not successful.
Query #1:
DECLARE @cols AS NVARCHAR(MAX);
SELECT @COLS = substring(list, 1, LEN(list) - 1)
FROM
(SELECT
list = (SELECT DISTINCT Product + ';'
FROM #tmp3
ORDER BY Product + ';'
FOR XML PATH(''), TYPE).value('.', 'nvarchar(MAX)')) AS T
SELECT @COLS
Query #2:
SELECT
STUFF((SELECT '; ' + Product
FROM
(SELECT DISTINCT TOP 100
Product,date
FROM #tmp3) x
ORDER BY date
FOR XML PATH('')), 1, 2, '') Text
I want output like above but bat should only come once.