使用row_number按行值对行进行SQL分区

时间:2017-03-10 10:51:34

标签: sql sql-server window-functions row-number

需要帮助对数据结果进行分区,以便通过结果列值获取行号 结果来自多个视图,但将使用下面的小表解释问题陈述。

create table example ( seqid bigint, itemid bigint , itemname varchar(50), itemdescription varchar(50), subitemname varchar(50), columncount bigint ) 

insert into example
select 1,1,'item1','itemdescription1','sub1',3 union all
select 2,1,'item1','itemdescription1','sub2',3 union all
select 3,1,'item1','itemdescription1','sub3',3 union all
select 4,1,'item1','itemdescription1','sub2.1',3 union all
select 5,1,'item1','itemdescription1','sub2.2',3 union all
select 6,1,'item1','itemdescription1','sub2.3',3 union all
select 7,2,'item2','itemdescription1','sub1',2 union all
select 8,2,'item2','itemdescription1','sub2',2 union all
select 9,3,'item3','itemdescription1','sub1',1 union all
select 10,3,'item3','itemdescription1','sub2',1 union all
select 11,3,'item3','itemdescription1','sub3',1 union all
select 12,3,'item3','itemdescription1','sub4',1 

选择如下所示

seqid   |itemid     |itemname   |itemdescription    |subitemname    |columncount
----------------------------------------------------------------------------------
1       |1      |item1      |itemdescription1   |sub1       |3
2       |1      |item1      |itemdescription1   |sub2       |3
3       |1      |item1      |itemdescription1   |sub3       |3
4       |1      |item1      |itemdescription1   |sub2.1     |3
5       |1      |item1      |itemdescription1   |sub2.2     |3
6       |1      |item1      |itemdescription1   |sub2.3     |3
7       |2      |item2      |itemdescription1   |sub1       |2
8       |2      |item2      |itemdescription1   |sub2       |2
9       |3      |item3      |itemdescription1   |sub1       |1
10      |3      |item3      |itemdescription1   |sub2       |1
11      |3      |item3      |itemdescription1   |sub3       |1
12      |3      |item3      |itemdescription1   |sub4       |1

并且需要对其进行分区,使得columncount列值表示每个项目的子项目的列数(toatal子项/列计数=总子项目行)(即,)具有2列计数的项目名称将具有2列用于行中的子项目。

上述数据的分区结果需要与RowId列一样,表示子项的行位置。

seqid   |itemid |itemname   |itemdescription    |subitemname    |fetchcount |RowID
----------------------------------------------------------------------------------
1   |1  |item1  |itemdescription1   |sub1   |3  |1
2   |1  |item1  |itemdescription1   |sub2   |3  |1
3   |1  |item1  |itemdescription1   |sub3   |3  |1
4   |1  |item1  |itemdescription1   |sub2.1 |3  |2
5   |1  |item1  |itemdescription1   |sub2.2 |3  |2
6   |1  |item1  |itemdescription1   |sub2.3 |3  |2
7   |2  |item2  |itemdescription1   |sub1   |2  |1
8   |2  |item2  |itemdescription1   |sub2   |2  |1
9   |3  |item3  |itemdescription1   |sub1   |1  |1
10  |3  |item3  |itemdescription1   |sub2   |1  |2
11  |3  |item3  |itemdescription1   |sub3   |1  |3
12  |3  |item3  |itemdescription1   |sub4   |1  |4

我尝试使用rank或row_number和cte窗口进行很多组合,但没有得到预期的结果。也不愿意使用循环或游标来更新子项的此RowID。请帮忙。

1 个答案:

答案 0 :(得分:0)

试试这个,这将适用于您提供的样本数据。

"pageInfo": {
  "totalResults": 1000000,
  "resultsPerPage": 5
 },