tesicg
tesicg

Reputation: 4053

How to select row with max value if the value of one column is the same?

Here's the query:

SELECT ActionTicketID, OrderTicketID, BarCode, MAX(ID) AS ID, TicketBarCode
FROM Table
where ActionTicketID = 26250347
GROUP BY ActionTicketID, OrderTicketID, BarCode, ID, TicketBarCode

The result looks as the following:

enter image description here

As you can see I get 3 rows, but need only one where ID is max. I think it is because of ActionTicketID is the same.

How to achieve it?

Upvotes: 1

Views: 48

Answers (1)

Praveen
Praveen

Reputation: 9335

Try order by;

SELECT top 1 ActionTicketID, OrderTicketID, BarCode, ID, TicketBarCode
FROM Table
where ActionTicketID = 26250347
order by ID desc

Upvotes: 1

Related Questions