﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / Dynamic top per group / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Fri, 24 May 2013 21:55:50 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Dynamic top per group</title><link>http://www.sqlservercentral.com/Forums/Topic1391472-392-1.aspx</link><description>You're welcome. Glad that worked for you. :-D</description><pubDate>Mon, 03 Dec 2012 08:33:14 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>RE: Dynamic top per group</title><link>http://www.sqlservercentral.com/Forums/Topic1391472-392-1.aspx</link><description>Thank you both. ROW_NUMBER ( ) OVER is what I needed.</description><pubDate>Mon, 03 Dec 2012 08:23:23 GMT</pubDate><dc:creator>Chrissy321</dc:creator></item><item><title>RE: Dynamic top per group</title><link>http://www.sqlservercentral.com/Forums/Topic1391472-392-1.aspx</link><description>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.[code]declare @Top intset @Top = 2;with cteCount as(	select TestGroup, TestData, ROW_NUMBER() over(partition by TestGroup order by TestData) as RowNum	from TestTable)select * from cteCountwhere RowNum &amp;lt;= @Top[/code]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'.[code]INSERT [dbo].[TestTable] (TestGroup,TestData) VALUES ('C',5)[/code]It will find and return only the 1 row for that group.</description><pubDate>Fri, 30 Nov 2012 12:20:31 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>RE: Dynamic top per group</title><link>http://www.sqlservercentral.com/Forums/Topic1391472-392-1.aspx</link><description>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.[url]http://msdn.microsoft.com/en-us/library/ms186734.aspx[/url] ROW_NUMBER()[url]http://msdn.microsoft.com/en-us/library/ms190766(v=sql.105).aspx[/url] Common Table Expressions.Also great sample data and ddl.</description><pubDate>Fri, 30 Nov 2012 11:20:27 GMT</pubDate><dc:creator>CapnHector</dc:creator></item><item><title>Dynamic top per group</title><link>http://www.sqlservercentral.com/Forums/Topic1391472-392-1.aspx</link><description>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	1A	2A	3B	1B	2B	3not A	1A	2A	3Thanks 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 returnDECLARE @SelectType varchar(6) --Either Top, Bottom or ALLSET @Top = 3SELECTTOP (@Top) 	TestGroup,	TestDataFROM [dbo].[TestTable]ORDER BY 	TestData</description><pubDate>Fri, 30 Nov 2012 11:11:13 GMT</pubDate><dc:creator>Chrissy321</dc:creator></item></channel></rss>