Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 select distinct and max with order by?

Author  Topic 

dirwin26
Yak Posting Veteran

81 Posts

Posted - 2006-06-19 : 11:57:25
I have monthly data in a table and I am trying to grab the max # of sales in each given month by each customer.
Here's what my table looks like:
Customer//month//sales
ABC//JAN//5
ABC//JAN//20
XYZ//JAN//2
XYZ//JAN//10

So the results of my query will be
ABC//JAN//20
XYZ//JAN//10

What I can't figure out is how to grab a distinct company within each month where the sales =MAX for that one customer.

Thanks

nr
SQLTeam MVY

12543 Posts

Posted - 2006-06-19 : 12:17:18
select Customer, Month, Sales = max(Sales)
from tbl
group by Customer, Month


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

dirwin26
Yak Posting Veteran

81 Posts

Posted - 2006-06-19 : 12:28:25
that did the job. Thanks.
Go to Top of Page
   

- Advertisement -