Advertisement
Guest User

Untitled

a guest
Aug 12th, 2017
1,783
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. email address | product code
  2. ----------------------+---------------
  3. garry123@gmail.com | A123A
  4. garry123@gmail.com | AB263
  5. ada121@hotmail.com | 45632A
  6. ada121@hotmail.com | 78YU
  7. garry123@gmail.com | 6543D
  8.  
  9. garry123@gmail.com | A123A,AB263,6543D
  10. ada121@hotmail.com | 45632A,78YU
  11.  
  12. SELECT
  13. c.EmailAddress,
  14. od.concat(productcode) as Product_SKU
  15. FROM
  16. OrderDetails od
  17. JOIN
  18. Orders o ON od.OrderID = o.OrderID
  19. JOIN
  20. Customers c ON c.CustomerID = o.CustomerID
  21. WHERE
  22. o.OrderDate BETWEEN 01/01/2016 AND GETDATE()
  23. GROUP BY
  24. c.EmailAddress
  25.  
  26. SELECT
  27. c.EmailAddress,
  28. productcode = STUFF((SELECT ',' + od.productcode
  29. FROM Orderdetails od
  30. WHERE c.EmailAddress = od.EmailAddress
  31. FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
  32. FROM
  33. OrderDetails od
  34. JOIN
  35. Orders o ON od.OrderID = o.OrderID
  36. JOIN
  37. Customers c ON c.CustomerID = o.CustomerID
  38. WHERE
  39. o.OrderDate BETWEEN 01/01/2016 AND GETDATE()
  40.  
  41. declare @test table
  42. (
  43. email varchar(50),
  44. address varchar(50)
  45. )
  46.  
  47. insert into @test VALUES
  48. ('garry123@gmail.com','A123A'),
  49. ('garry123@gmail.com','AB263'),
  50. ('ada121@hotmail.com','45632A'),
  51. ('ada121@hotmail.com','78YU'),
  52. ('garry123@gmail.com','6543D')
  53.  
  54. SELECT DISTINCT
  55. email,
  56. Stuff((SELECT ', ' + address
  57. FROM @test t2
  58. WHERE t2.email = t1.email
  59. FOR XML PATH('')), 1, 2, '') Address
  60. FROM @test t1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement