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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/