John
John

Reputation: 475

How do i filter a sql table by the TOP 1?

I have a query that is filtered by SO Number. It also has a column that has a unique number generated each time the SO is updated. How can i alter my code so that not only will it be filtered by the SO Number, but also filter by the TOP 1, or highest count of the updated key?

Thank you! This is on SQL Server. Should have specified earlier

Upvotes: 1

Views: 1959

Answers (2)

Kev Ritchie
Kev Ritchie

Reputation: 1647

SELECT TOP 1 SONumber
FROM ExampleTable
ORDER BY SONumber DESC

Or

SELECT MAX(SONumber)
FROM ExampleTable

Upvotes: 0

Dan
Dan

Reputation: 10786

SELECT whatever_you_want
    FROM whereever_it_is
    WHERE your_criteria
    ORDER BY so_number DESC
    LIMIT 1

which will give you the "highest" so_number, returning only one record even if there are several with the same value

or

SELECT whatever_you_want
    FROM whereever_it_is
    WHERE your_criteria
        AND so_number == MAX(so_number)

which will give all rows with that maximum value, returning all if there are more than one.

Upvotes: 4

Related Questions