Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- email address | product code
- ----------------------+---------------
- garry123@gmail.com | A123A
- garry123@gmail.com | AB263
- ada121@hotmail.com | 45632A
- ada121@hotmail.com | 78YU
- garry123@gmail.com | 6543D
- garry123@gmail.com | A123A,AB263,6543D
- ada121@hotmail.com | 45632A,78YU
- 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
- 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()
- declare @test table
- (
- email varchar(50),
- address varchar(50)
- )
- insert into @test VALUES
- ('garry123@gmail.com','A123A'),
- ('garry123@gmail.com','AB263'),
- ('ada121@hotmail.com','45632A'),
- ('ada121@hotmail.com','78YU'),
- ('garry123@gmail.com','6543D')
- SELECT DISTINCT
- email,
- Stuff((SELECT ', ' + address
- FROM @test t2
- WHERE t2.email = t1.email
- FOR XML PATH('')), 1, 2, '') Address
- FROM @test t1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement