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 2012 Forums
 Transact-SQL (2012)
 Top 20 records

Author  Topic 

chiragvm
Yak Posting Veteran

65 Posts

Posted - 2014-08-02 : 02:42:24
Hi to all

need help to select top 20 records from table

i have a table in which approx 10000 records associated with userId their is only 2 column ProjectId,userid, one user can have multiple projects

i want to write down a query which returns me user wise top 20 records
user 1 has 20 project user 2 has 20 project

assume that i have 5 users and each user has 50 projects, then query will return me a 100 rows each user has 20 records

-------------
Chirag
India
Sr.Software Engineer

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-08-02 : 05:47:12
[code]
SELECT userid,projectid
FROM
(
SELECT *,ROW_NUMBER() OVER (PARTITION BY userid ORDER BY projectid) AS Seq
)t
WHERE Seq <= 20
ORDER BY userid,seq
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -