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
----------------------+---------------
garry123@gmail.com | A123A
garry123@gmail.com | AB263
ada121@hotmail.com | 45632A
ada121@hotmail.com | 78YU
garry123@gmail.com | 6543D
and I want this result:
garry123@gmail.com | A123A,AB263,6543D
ada121@hotmail.com | 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'.