September 20, 2008 at 11:00 pm
Hi, Anyone can help?
How can I show the output of this loop in one recordset?
Another words, I want UNION the output in a single recordset. Please help. Thanks
DECLARE @cn AS int
set @cn = 0
while @cn < 10
begin
select rank ()over (order by budperc desc) as rank, * from table1
where id =@cn+1
set @cn = @cn+1
end
September 21, 2008 at 9:54 am
This returns a single recordset ... and might be what you are looking for
took this example from BOL
USE AdventureWorks;
GO
SELECT
RANK() OVER(ORDER BY SalesYTD DESC) AS 'Rank'
,s.SalesYTD,s.salespersonid
FROM Sales.SalesPerson s
WHERE s.salespersonid < 280
Notice there is no looping - hope this helps
September 21, 2008 at 10:01 am
With the info given, there's no need for a loop.
SELECT RANK() OVER(PARTITION BY ID ORDER BY BudPerc DESC) AS Rank, *
FROM Table1
WHERE ID BETWEEN 1 AND 10
The PARTITION BY will do the trick so far as restarting the RANK for each ID.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 21, 2008 at 10:26 am
It works now. As you said the trick was in Partition BY clause to restart the ranking.. Awesome!! Thanx a lot..
September 21, 2008 at 10:32 am
You bet. Thanks a bunch for the feedback. 🙂
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply