SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Dynamic top per group


Dynamic top per group

Author
Message
Chrissy321
Chrissy321
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3162 Visits: 4794
Hello All,

I'd like to specify a dynamic Top criteria and return the top number of records per group.

My desired results is:

A 1
A 2
A 3
B 1
B 2
B 3

not

A 1
A 2
A 3

Thanks if you can help.

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TestTable]') AND type in (N'U'))
DROP TABLE [dbo].[TestTable]

CREATE TABLE [dbo].[TestTable] (TestGroup char(1),TestData int)
INSERT [dbo].[TestTable] (TestGroup,TestData) VALUES ('A',1)
INSERT [dbo].[TestTable] (TestGroup,TestData) VALUES ('A',2)
INSERT [dbo].[TestTable] (TestGroup,TestData) VALUES ('A',3)
INSERT [dbo].[TestTable] (TestGroup,TestData) VALUES ('A',4)
INSERT [dbo].[TestTable] (TestGroup,TestData) VALUES ('A',5)


INSERT [dbo].[TestTable] (TestGroup,TestData) VALUES ('B',1)
INSERT [dbo].[TestTable] (TestGroup,TestData) VALUES ('B',1)
INSERT [dbo].[TestTable] (TestGroup,TestData) VALUES ('B',1)
INSERT [dbo].[TestTable] (TestGroup,TestData) VALUES ('B',1)
INSERT [dbo].[TestTable] (TestGroup,TestData) VALUES ('B',1)


DECLARE @Top int --The number of records to return
DECLARE @SelectType varchar(6) --Either Top, Bottom or ALL

SET @Top = 3


SELECT
TOP (@Top)
TestGroup,
TestData
FROM [dbo].[TestTable]
ORDER BY
TestData
CapnHector
CapnHector
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3263 Visits: 1789
Have a look at ROW_NUMBER() OVER() and Common Table Expressions. These will allow you to accomplish what you need to do. If you need any help let us know and show us what you have.

http://msdn.microsoft.com/en-us/library/ms186734.aspx ROW_NUMBER()

http://msdn.microsoft.com/en-us/library/ms190766(v=sql.105).aspx Common Table Expressions.

Also great sample data and ddl.


For faster help in answering any problems Please read How to post data/code on a forum to get the best help - Jeff Moden for the best way to ask your question.

For performance Issues see how we like them posted here: How to Post Performance Problems - Gail Shaw

Need to Split some strings? Jeff Moden's DelimitedSplit8K
Jeff Moden's Cross tab and Pivots Part 1
Jeff Moden's Cross tab and Pivots Part 2
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62681 Visits: 17959
With such a great job posting ddl and sample data I think you deserve a complete answer. I wish more people would make their posts so easy to work with.


declare @Top int
set @Top = 2

;with cteCount as
(
select TestGroup, TestData, ROW_NUMBER() over(partition by TestGroup order by TestData) as RowNum
from TestTable
)

select * from cteCount
where RowNum <= @Top



Now all you have to do is change the value of @Top and the number of rows per group will change. It will also work if @Top specified finds fewer rows for any given group. Say you added a group 'C'.


INSERT [dbo].[TestTable] (TestGroup,TestData) VALUES ('C',5)



It will find and return only the 1 row for that group.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Chrissy321
Chrissy321
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3162 Visits: 4794
Thank you both. ROW_NUMBER ( ) OVER is what I needed.
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62681 Visits: 17959
You're welcome. Glad that worked for you. :-D

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search