我有一个查询,它返回具有组类别的数据和一些细节,如下所示:
Category | Title
==================
cat1 --- titlex
cat1 --- titley
cat2 --- titley
cat3 --- titlez
cat3 --- titlex
cat4 --- titlex
我想显示一个在外部组(类别)上有行号的表,如下所示:
RN | Category | Title
======================
1 cat1
titlex
titley
2 cat2
titley
3 cat3
titlez
titlex
4 cat4
titlex
问题是,当我在 sql 查询或 ROWNUMBER SSRS 函数中添加 RN 列作为 ROW_NUMBER 时(尝试 NOTHING、Group 和 Details 作为范围,以防万一),我总是得到 RN 列的 2 1 2 或 1 3 4 6 之类的数字.
编辑 Sql 查询(为简单起见更改了表名和属性)
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