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)
 How to get the second highest using TOP

Author  Topic 

jerrinj
Starting Member

4 Posts

Posted - 2001-08-06 : 07:03:11
Hello Sql masters,

I just want a simple solution using TOP predicate.

I have 2 tables - a master and a detail.

TableA
orderId customer
------- --------
1 Balaji
2 Adarshkumar
3 Selma
4 Badri

TableB
orderId itemNo
------- ------
1 1001
1 1003
1 1010
2 1003
3 1004
3 1010
3 1011
3 1020
4 1001

I want the resultset to have the orderId, customer and the number of times the orderId is present in TableB.

The resultset will have the record of the person who has the second highest number of items in his order. It will look something like given below:

orderId customer no_of_items
------- -------- -----------
1 Balaji 3


How to achieve the above result?

   

- Advertisement -