我想到了2种方式,一种是MATRIX风格的报表,其中列组是你的列,另一种是普通的表,你把数据JOIN出来3份,分别是姓名、ID和其他你想要的字段。MATRIX风格无疑是更优雅和灵活的,但如果你要把报表交给高级用户,普通表可能更容易被客户修改。
这两种解决方案都是先用PAGE、ROW和COLUMN信息标记数据。请注意,我是按NAME排序的,但你可以按任何字段排序。还要注意的是,这个解决方案并不依赖于你的ID是顺序的,也不依赖于你想要的顺序,它根据NAME或你选择的任何其他内容生成自己的顺序号。
在这个演示中,我将RowsPerPage和NumberofColumns设置为硬编码常数,但如果你使用MATRIX格式,它们可以很容易地成为用户选择的参数。
DECLARE @RowsPerPage INT = 20
DECLARE @Cols INT = 3
;with
--Fake data generation BEGIN
cteSampleSize as (SELECT TOP 70 ROW_NUMBER () OVER (ORDER BY O.name) as ID
FROM sys.objects as O
), cteFakeData as (
SELECT N.ID, CONCAT(CHAR(65 + N.ID / 26), CHAR(65 + ((N.ID -1) % 26))
--, CHAR(65 + ((N.ID ) % 26))
) as Name
FROM cteSampleSize as N
),
--Fake data generation END, real processing begins below
cteNumbered as ( -- We can't count on ID being sequential and in the order we want!
SELECT D.*, ROW_NUMBER () OVER (ORDER BY D.Name) as SeqNum
--Replace ORDER BY D.Name with ORDER BY D.{Whatever field}
FROM cteFakeData as D --Replace cteFakeData with your real data source
), ctePaged as (
SELECT D.*
, 1+ FLOOR((D.SeqNum -1) / (@RowsPerPage*@Cols)) as PageNum
, 1+ ((D.SeqNum -1) % @RowsPerPage) as RowNum
, 1+ FLOOR(((D.SeqNum-1) % (@RowsPerPage*@Cols) ) / @RowsPerPage) as ColNum
FROM cteNumbered as D
)
--FINAL - use this for MATRIX reports (best)
SELECT * FROM ctePaged ORDER BY SeqNum
如果你想在普通的表中使用JOIN方法来允许这样做,用这个查询代替上面的--FINAL查询。需要注意的是,这是很细微的,所以在最终的报告中用几度的满度来测试。我用70行和90行的样本数据进行了测试,所以我有部分第一列和完整的第一列和部分第二列。
--FINAL - use this for TABLE reports (simpler)
SELECT C1.PageNum , C1.RowNum , C1.ID as C1_ID, C1.Name as C1_Name
, C2.ID as C2_ID, C2.Name as C2_Name
, C3.ID as C3_ID, C3.Name as C3_Name
FROM ctePaged as C1 LEFT OUTER JOIN ctePaged as C2
ON C1.PageNum = C2.PageNum AND C1.RowNum = C2.RowNum
AND C1.ColNum = 1 AND (C2.ColNum = 2 OR C2.ColNum IS NULL)
LEFT OUTER JOIN ctePaged as C3 ON C1.PageNum = C3.PageNum
AND C1.RowNum = C3.RowNum AND (C3.ColNum = 3 OR C3.ColNum IS NULL)
WHERE C1.ColNum = 1
1)用下面的查询添加数据集,得到Page号和Table号。你可以根据需要改变20和60的数量。在我的例子中,我需要每节20条记录,有3个节,所以每页总记录数为60条。
Select *,(ROW_NUMBER ( ) OVER ( partition by PageNumber order by Id )-1)/20 AS TableNumber from (
Select (ROW_NUMBER ( ) OVER ( order by Id )-1)/60 AS PageNumber
,* from Numbers
)Src
2)添加一列表,选择准备好的数据集。
3)添加 PageNumber
中的组表达式为详细信息组。
4)右击细节行,添加列父组。选择组,通过 TableNumber
.
6) 再增加一个表,选择ID和名称。
7) 将这个新创建的表拖入之前创建的表的单元格中。并增加表的大小。