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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieve only top record using IN 1

Status
Not open for further replies.

Zoom1234

Programmer
Oct 30, 2003
116
BE
Hi,
I have 'Order' table which has many account nos for a particluar tel no.
So if i query
Code:
select * from orders where telno = '1234' order by inid;
i can get one or more than one records.
Now i have a set of telnos for which i want to retreive records for each telno with inid as max.
Code:
select * from orders where telno in ('1234','5678',...)

How do i do it.

TIA


 
Do you mean you want to get for each telno value the orders record with the highest value of inid? If so, you could use:
[tt]
SELECT orders.*
FROM
(
SELECT telno,MAX(inid) m
FROM orders
WHERE telno IN ('1234','5678')
GROUP BY telno
)
sq
JOIN orders o ON sq.telno=o.telno AND sq.m=o.inid
[/tt]
 
Thanks TonyGroves,
Query works perfectly except i am getting 19 records instead of 20. May be the data is not consistent. I will check it out.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top