22,209
社区成员
发帖
与我相关
我的任务
分享
--完整如下:
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([line] int,[count] int,[serial] int)
insert [tb]
select 1,2,22 union all
select 1,3,23 union all
select 2,7,24
--------------开始查询--------------------------
select line,row_number()over(partition by line order by line),serial
from
[tb] a
left join
master..spt_values c
on
c.number<=a.[count]
and
c.number>0
and
c.[type]='p'
--修改小F大大的
select line,row_number()over(partition by line order by line),serial
from
[tb]a
left join
master..spt_values c
on
c.number<=a.[count]
and
c.number>0
and
c.[type]='p'
IF EXISTS (SELECT 1 FROM SYSOBJECTS WHERE name = 'tb')
BEGIN
DROP TABLE tb
END
GO
create table tb(col1 int,col2 int,col3 varchar(10))
insert into tb
select 1,1,'a' union
select 1,2,'b' union
select 2,4,'c'
select a.col1,b.number,a.col3
from tb a,master..spt_values b
where a.col2 <= b.number and number < (select MIN(col2) from tb where col3 > a.col3) and b.type='p'
UNION
select a.col1,b.number,a.col3
from (SELECT rn=ROW_NUMBER() OVER (PARTITION BY col1 ORDER BY col2),col1,col2,col3 FROM tb) a,master..spt_values b
where b.type='p' AND rn = 1 AND number <= col2 AND number > 0
col1 number col3
1 1 a
1 2 b
1 3 b
2 1 c
2 2 c
2 3 c
2 4 c
create table tb(col1 int,col2 int,col3 varchar(10))
insert into tb
select 1,1,'a' union
select 1,2,'b' union
select 2,4,'c'
select a.col1,b.number+1,a.col3
from tb a,spt_values b where a.col2>b.number
and b.type='p'
/*
col1 col3
----------- ----------- ----------
1 1 a
1 1 b
1 2 b
2 1 c
2 2 c
2 3 c
2 4 c
(7 行受影响)