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)
 Getting multiple, grouped "TOP 10"s

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-11-10 : 07:31:31
Brad writes "Say you have a table with the following structure:

CallLog
--------
ID
callerPhoneNumber
calleePhoneNumber
calledOn

1.) What would be the best way to go about getting the last 10 calls (with all of the fields) per [calleePhoneNumber] in a single execution?

2.) Would you change the query if CallLog were manually partioned based on the month and year of the [calledOn] date (with no view encompassing the partition) (e.g. CallLog_01_05, CallLog_02_05, etc.)? In my situation, I'd like to have a query that goes back a few months and my tables are pretty big (after the partition).

I'm using SQL Server 2000.

Thanks,
Brad"

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2005-11-10 : 07:43:50
Well for that you can create the stored procedure for the same..

and Can pass the parameters of the months etc..

Create Proc GetCallLog
(
@pMonth smallint = 0
@pNumber Varchar(20)
)
As
Begin
Select Top 10 * From CallLog Where CalleePhoneNumber = @pNumber And
(Month(CalledOn) = @pMonth And @pMonth =0)

End

Hope this helps..


Complicated things can be done by simple thinking
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-11-10 : 08:06:59
It depends on how you define LAST
As suggested use Top 10 and order by DESC

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -