sql group by top


/ Published in: SQL
Save to your folder(s)



Copy this code and paste it in your HTML
  1. SELECT
  2. c.FirstName,
  3. c.LastName,
  4. o.OrderDate,
  5. o.OrderAmount
  6. FROM Customers c
  7. JOIN Orders o ON o.CustomerId = c.CustomerId
  8. WHERE
  9. o.OrderDate =
  10. (
  11. SELECT TOP(1)
  12. o1.OrderDate
  13. FROM Orders o1
  14. WHERE
  15. o1.CustomerId = o.CustomerId
  16. ORDER BY
  17. o1.OrderDate DESC
  18. )

URL: http://sqlblog.com/blogs/adam_machanic/archive/2008/02/08/who-s-on-first-solving-the-top-per-group-problem-part-1-technique.aspx

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.