Posted By

samwa on 07/05/11


Tagged

sql group


Versions (?)

sql group by top


 / Published in: SQL
 

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

  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. )

Report this snippet  

You need to login to post a comment.