Add row number to outer group in t-sql or ssrs
-
I have a query that returns data with group category and some details like this: Category | Title ================== cat1 --- titlex cat1 --- titley cat2 --- titley cat3 --- titlez cat3 --- titlex cat4 --- titlex I want to display a table that has row number on outer group (Category) like this: RN | Category | Title ====================== 1 cat1 titlex titley 2 cat2 titley 3 cat3 titlez titlex 4 cat4 titlex The problem is, when I add RN column as ROW_NUMBER in sql query or ROWNUMBER SSRS function (tried NOTHING, Group and Details as a scope, just in case), I always get numbers like 2 1 2 or 1 3 4 6 for RN column. EDIT Sql Query (table names and properties changed for simplicity) SELECT -- this rownumber does not work, counts every occurrence of category --ROW_NUMBER() OVER ( --PARTITION BY c.Name -- tried this too, this resets on each cat --ORDER BY c.Name) AS RN, c.Name, p.Name FROM Products p INNER JOIN Categories c ON p.CategoryId = c.Id GROUP BY c.Name, p.Name ORDER BY c.Name, p.Name
-
Answer:
You don't want the row numbers (as you've observed, the row numbers are assigned to every... um... row). Maybe you want http://msdn.microsoft.com/en-us/library/ms173825.aspx? SELECT DENSE_RANK() OVER (ORDER BY c.Name) AS RN, c.Name, p.Name FROM Products p INNER JOIN Categories c ON p.CategoryId = c.Id GROUP BY c.Name, p.Name ORDER BY c.Name, p.Name As to your desired output, I wouldn't attempt to achieve that in SQL - use a reporting/formatting tool to get the final layout.
Goran Obradovic at Stack Overflow Visit the source
Other answers
You can also accomplish this at the presentation layer in SSRS: =RunningValue(Fields!CategoryFieldName.Value, CountDistinct, Nothing)
Jamie F
Related Q & A:
- How to add primary key from multiple table as a foreign key in a table in sql server 2008?Best solution by stackoverflow.com
- How to add new row to a databound gridview?Best solution by Stack Overflow
- How do you add a member to your group?Best solution by Yahoo! Answers
- How do I add another Yahoo group to my Yahoo group?Best solution by Yahoo! Answers
- How do I add music to my Yahoo Group?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.