Advertisement
flipworldit

SQL. Statistic by products. Before formating

Sep 17th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 0.63 KB | None | 0 0
  1. WITH Orders as ( SELECT [UserId],name as [Product],[date] FROM [dbo].[Order] o join [dbo].OrderDetails od on o.id = od.orderId join [dbo].Product p join od.productId = p.id
  2. where o.id in (SELECT max(o.id) FROM [dbo].[Order] o join [dbo].OrderDetails od on o.id = od.orderId join [dbo].Product p join od.productId = p.id group by userId, [Product])
  3. )
  4. select UsersView.Id, MAX(CASE WHEN Orders.[Product] = 'Смузи' THEN Orders.[date] ELSE NULL END) AS smuzi, MAX(CASE WHEN Orders.[Product] = 'Стейк' THEN Orders.[date] ELSE NULL END) AS steak
  5. FROM UsersView LEFT OUTER JOIN Orders on UsersView.Id = Orders.UserId
  6. GROUP BY UserView.Id
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement