How to fetch column on the basis of Row count using while loop

Posted by Pkanwar under Sql Server on 4/11/2013 | Points: 10 | Views : 1809 | Status : [Member] | Replies : 1
Hi.

We have query on count basis like:
select @intCount= count(*) from TableName.
While(@intCount>0)
Begin
SET @SQL='SELECT [USER],COUNT(ID) AS FCount,MAX(TimeWritten) As MaxDT'
SET @SQL=@SQL+ ' FROM '+@TABLENAME
set @intCount=@intCount-1
End

Now in place of @TABLENAME, I want to show fetch row count of columns in table.
For exp : We found count 3, then first fetch column of third row in table, then second row.. so on..

Thanks




Responses

Posted by: Bandi on: 9/10/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Hi,
SELECT [USER],COUNT(ID) AS FCount,MAX(TimeWritten) As MaxDT'  

FROM TABLENAME
GROUP BY [USER]
ORDER BY Fcount DESC

The above query will display the records in descending order of "count of records"

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Pkanwar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response