谢谢表1id 号码 数据1 数据2 数据3
1 1 7 8 6
2 2 2 4 5
3 3 2 1 1
4 4 2 3 4
.
20 20
21 1
22 2
。。id是自然排列的,号码列都是是1-20循环的,想把1-20合并,就是1+2+3+。。20就是每20行和成1行得表2 号码 数据
1 786245211234.
2.。。
1 1 7 8 6
2 2 2 4 5
3 3 2 1 1
4 4 2 3 4
.
20 20
21 1
22 2
。。id是自然排列的,号码列都是是1-20循环的,想把1-20合并,就是1+2+3+。。20就是每20行和成1行得表2 号码 数据
1 786245211234.
2.。。
;with cte as(
select id,(id-号码)/20+1 id1,号码,ltrim(数据1)+ltrim(数据2)+ltrim(数据3) sj from 表1
)
select a.id1,a.sj+b.sj+c.sj+d.sj+...+s.sj
from cte a inner join cte b on a.id1=b.id1 and a.号码=b.号码-1
inner join cte c on a.id1=c.id1 and a.号码=c.号码-2
inner join cte d on a.id1=d.id1 and a.号码=d.号码-3
...
inner join cte s on a.id1=s.id1 and a.号码=s.号码-19
create table #表1
(id int identity(1,1),号码 int, 数据1 int,数据2 int,数据3 int)
insert #表1
select 1 ,7 ,8 ,6 union all
select 2 ,2 ,4 ,5 union all
select 3 ,2 ,1 ,1 union all
select 4 ,2 ,3 ,4 union all
select 5 ,7 ,8 ,6 union all
select 6 ,2 ,4 ,5 union all
select 7 ,2 ,1 ,1 union all
select 8 ,2 ,3 ,4 union all
select 9 ,7 ,8 ,6 union all
select 10 ,2 ,4 ,5 union all
select 11 ,2 ,1 ,1 union all
select 12 ,2 ,3 ,4 union all
select 13 ,7 ,8 ,6 union all
select 14 ,2 ,4 ,5 union all
select 15 ,2 ,1 ,1 union all
select 16 ,2 ,3 ,4 union all
select 17 ,7 ,8 ,6 union all
select 18 ,2 ,4 ,5 union all
select 19 ,2 ,1 ,1 union all
select 20 ,8 ,8 ,9 union allselect 1 ,7 ,8 ,6 union all
select 2 ,2 ,4 ,5 union all
select 3 ,2 ,1 ,1 union all
select 4 ,2 ,3 ,4 union all
select 5 ,7 ,8 ,6 union all
select 6 ,2 ,4 ,5 union all
select 7 ,2 ,1 ,1 union all
select 8 ,2 ,3 ,4 union all
select 9 ,7 ,8 ,6 union all
select 10 ,2 ,4 ,5 union all
select 11 ,2 ,1 ,1 union all
select 12 ,2 ,3 ,4 union all
select 13 ,7 ,8 ,6 union all
select 14 ,2 ,4 ,5 union all
select 15 ,2 ,1 ,1 union all
select 16 ,2 ,3 ,4 union all
select 17 ,7 ,8 ,6 union all
select 18 ,2 ,4 ,5 union all
select 19 ,2 ,1 ,1 union all
select 20 ,3 ,4 ,5 select Row_number()over(order by getdate()) as 号码,
sum(数据1) as 数据1,sum(数据2) as 数据2,sum(数据3) as 数据3
from #表1 group by 号码-id
create table tb(id int,号码 int,数据1 int,数据2 int, 数据3 int)
insert tb
select 1 ,1 ,7 ,8 ,6 union all
select 2 ,2 ,2 ,4 ,5 union all
select 3 ,3 ,2 ,1 ,1 union all
select 4 ,4 ,2 ,3 ,4 union all
select 5 ,1 ,7 ,8 ,6 union all
select 6 ,2 ,7 ,8 ,6
gocreate view v_tb
as
select (id-1)/4 as id,ltrim(数据1)+ltrim(数据2)+ltrim(数据3) as 数据 from tb
goCREATE FUNCTION dbo.f_str(@id int)
RETURNS varchar(8000)
AS
BEGIN
DECLARE @r varchar(8000)
SET @r = ''
SELECT @r = @r + 数据 FROM v_tb WHERE id=@id
RETURN STUFF(@r, 1, 1, '')
END
GOselect id,dbo.f_str(id) from v_tb group by iddrop function dbo.f_str
drop view v_tb
drop table tb --把上面(id-1)/4改为(id-1)/20即可
(id int identity(1,1),号码 int, 数据1 int,数据2 int,数据3 int)
insert 表1
select 1 ,7 ,8 ,6 union all
select 2 ,2 ,4 ,5 union all
select 3 ,2 ,1 ,1 union all
select 4 ,2 ,3 ,4 union all
select 5 ,7 ,8 ,6 union all
select 6 ,2 ,4 ,5 union all
select 7 ,2 ,1 ,1 union all
select 8 ,2 ,3 ,4 union all
select 9 ,7 ,8 ,6 union all
select 10 ,2 ,4 ,5 union all
select 11 ,2 ,1 ,1 union all
select 12 ,2 ,3 ,4 union all
select 13 ,7 ,8 ,6 union all
select 14 ,2 ,4 ,5 union all
select 15 ,2 ,1 ,1 union all
select 16 ,2 ,3 ,4 union all
select 17 ,7 ,8 ,6 union all
select 18 ,2 ,4 ,5 union all
select 19 ,2 ,1 ,1 union all
select 20 ,9 ,9 ,10 union allselect 1 ,7 ,8 ,6 union all
select 2 ,2 ,4 ,5 union all
select 3 ,2 ,1 ,1 union all
select 4 ,2 ,3 ,4 union all
select 5 ,7 ,8 ,6 union all
select 6 ,2 ,4 ,5 union all
select 7 ,2 ,1 ,1 union all
select 8 ,2 ,3 ,4 union all
select 9 ,7 ,8 ,6 union all
select 10 ,2 ,4 ,5 union all
select 11 ,2 ,1 ,1 union all
select 12 ,2 ,3 ,4 union all
select 13 ,7 ,8 ,6 union all
select 14 ,2 ,4 ,5 union all
select 15 ,2 ,1 ,1 union all
select 16 ,2 ,3 ,4 union all
select 17 ,7 ,8 ,6 union all
select 18 ,2 ,4 ,5 union all
select 19 ,2 ,1 ,1 union all
select 20 ,3 ,4 ,5 create function Getstr(@gid int)returns nvarchar(100)
as
begin
Declare @str nvarchar(100)
set @str=''
select @str=@str+ltrim(数据1)+ltrim(数据2)+ltrim(数据3) from 表1
where 号码-id =@gid
return @str
endselect 1-(号码-id)/20 as 号码 ,dbo.Getstr(号码-id) as 数据 from 表1
create function Getstr(@gid int)returns nvarchar(100)
as
begin
Declare @str nvarchar(100)
set @str=''
select @str=@str+ltrim(数据1)+ltrim(数据2)+ltrim(数据3) from 表1
where 号码-id =@gid
return @str
endselect distinct 1-(号码-id)/20 as 号码 ,dbo.Getstr(号码-id) as 数据 from 表1