I know group_concat
doesn't work in SQL Server 2008, but I want to do group_concat
.
My sample data looks like this:
email address | product code
----------------------+---------------
[email protected] | A123A
[email protected] | AB263
[email protected] | 45632A
[email protected] | 78YU
[email protected] | 6543D
and I want this result:
[email protected] | A123A,AB263,6543D
[email protected] | 45632A,78YU
Code I have tried:
SELECT
c.EmailAddress,
od.concat(productcode) as Product_SKU
FROM
OrderDetails od
JOIN
Orders o ON od.OrderID = o.OrderID
JOIN
Customers c ON c.CustomerID = o.CustomerID
WHERE
o.OrderDate BETWEEN 01/01/2016 AND GETDATE()
GROUP BY
c.EmailAddress
I get an error:
Cannot find either column "od" or the user-defined function or aggregate "od.concat", or the name is ambiguous.
But this is not working. Can anyone please tell me correct way of doing this?
Code I am trying after editing:
SELECT
c.EmailAddress,
productcode = STUFF((SELECT ',' + od.productcode
FROM Orderdetails od
WHERE c.EmailAddress = od.EmailAddress
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
FROM
OrderDetails od
JOIN
Orders o ON od.OrderID = o.OrderID
JOIN
Customers c ON c.CustomerID = o.CustomerID
WHERE
o.OrderDate BETWEEN 01/01/2016 AND GETDATE()
Now I'm getting this error:
Invalid column name 'EmailAddress'.