Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with query

Status
Not open for further replies.

pwomack

Technical User
Apr 16, 2005
121
US
I have the following data in a table:

order # OpCode Date
12345 50 5/17/09
12345 51 6/12/09
12345 52 6/27/09

I want to write a query that returns only 1 row; the row with the maximum OpCode for an order #. So, I want to return OpCode 52 with Date 6/27/09.

I'll have the value of the order # for the query.

Thanks.
 
Code:
SELECT YourTable.*
FROM YourTable
INNER JOIN (SELECT Order, MAX(OpCode) AS OpCode
                   FROM YourTable
            GROUP BY Order) MaxOrd
     ON YourTable.Order  = MaxOrd.Order AND
        YourTable.OpCode = MaxOrd.OpCode

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
SQL Server 2005 and up

select Order, OptCode, Date from (select *, row_number() over (partition by Order order by Date DESC) as RowNumber from myTable) OrderedSet where RowNumber = 1
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top