We help IT Professionals succeed at work.

How to find the mximum number in a rowset?

Last Modified: 2010-08-05
There is a Table called Sales and the table holds columns(Sales_no,Sales_order,Orderdate)

For example

Sales_no  Sales_order  Orderdate
100           1                   7/18/2007
100           2                   8/23/2007
100           3                   10/08/2007
200           1                   6/19/2007
200           2                   8/25/2007
200           3                   9/12/2007
200           4                   11/25/2007

How to retrieve maximum sales _order associated with sales_no and order_date?


Sales_no  Sales_order  Orderdate
100           3                   10/08/2007
200           4                   11/25/2007

Watch Question

This should do what you need.

select s.* from sales s
join (select sales_no,max(sales_order) as sales_order from Sales) sm
on s.sales_no=sm.sales_no 
and s.sales_order = sm.sales_order.

Open in new window


The above query doesn't works.

It states the error below

Column 'Sales.sales_no' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
This one is on us!
(Get your first solution completely free - no credit card required)
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.