Cybercrux

Everything is achievable through technology

Temptable

DECLARE @temptable TABLE
( id int IDENTITY(1,1)
,name nvarchar(20) NULL
,category int NOT NULL)
INSERT INTO @temptable ( name ,category)
VALUES
( 'nikhil1' ,1)
, ('sumesh1' ,2)
, ('sumesh2' ,2)
, ('nikhil2' ,1)
, ('nikhil3',1)
, ('sumesh3' ,2)
SELECT *
FROM @temptable t
ORDER BY t.category
;WITH CategoriesCTE AS
(
SELECT *, ROW_NUMBER() OVER(PARTITION BY category ORDER BY id) AS counts
FROM @temptable
)
SELECT *
FROM CategoriesCTE t
WHERE counts<=3

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s