现有一表表中的内容是这样的:
a1 a2
1 2
2 3
3 4
6 7
7 8
8 9
10 11
最后运行结果是这样的:
a1 a2 a3 a4
1 2 3 4
6 7 8 9
10 11 NULL NULL
请大哥哥,大姐姐帮帮忙代码该怎么写啊?
a1 a2
1 2
2 3
3 4
6 7
7 8
8 9
10 11
最后运行结果是这样的:
a1 a2 a3 a4
1 2 3 4
6 7 8 9
10 11 NULL NULL
请大哥哥,大姐姐帮帮忙代码该怎么写啊?
from tb A left join tb B
on A.a2=B.a1+1
if object_id('tempdb.dbo.#') is not null drop table #
create table #(a1 int, a2 int)
insert into #
select 1, 2 union all
select 2, 3 union all
select 3, 4 union all
select 6, 7 union all
select 7, 8 union all
select 8, 9 union all
select 10, 11;with cte as
(
select id=row_number()over(order by a)-1, * from (select a1 a from # union select a2 from #) t
)
select
a1 = max(case id%4 when 0 then a end),
a2 = max(case id%4 when 1 then a end),
a3 = max(case id%4 when 2 then a end),
a4 = max(case id%4 when 3 then a end)
from cte group by id/4/*
a1 a2 a3 a4
----------- ----------- ----------- -----------
1 2 3 4
6 7 8 9
10 11 NULL NULL
*/
要是这几个简单简单的数据就好办啦!
实际问题是这样的
母件 子件
r01 r02
r02 r03
r03 r04
r06 r07
r07 r08
r10 r11
现在我想要的结果是:
母件 子件1 子件2 子件3 子件4
r01 r02 r03 r04
r06 r07 r08 NULL
r10 r11 NULL NULL
if OBJECT_ID('tb') is not null
drop table tb;
go
create table tb (parent char(3),child char(3));
go
insert into tb
select 'r01','r02' union all select 'r02','r03' union all
select 'r03','r04' union all select 'r06','r07' union all
select 'r07','r08' union all select 'r10','r11';
go;with t as(
select parent,child,parent grp from tb where parent not in (select child from tb)
union all
select tb.parent,tb.child,t.grp from tb,t where tb.parent=t.child
)
select grp parent,[1] child1,[2] child2,[3] child3
from (select grp,child,row_number() over (partition by grp order by child) col from t) p
pivot (max(child) for col in ([1],[2],[3])) pvt;
/*
parent child1 child2 child3
------ ------ ------ ------
r01 r02 r03 r04
r06 r07 r08 NULL
r10 r11 NULL NULL
*/
如果子件数量不确定,则需要使用动态 sql 实现,不过基本方法相似。
/*
标题:SQL SERVER 2000中查询指定节点及其所有子节点的函数(字符串形式显示)
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间:2010-02-02
地点:新疆乌鲁木齐
*/--生成测试数据
create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
insert into tb values('001' , null , '广东省')
insert into tb values('002' , '001' , '广州市')
insert into tb values('003' , '001' , '深圳市')
insert into tb values('004' , '002' , '天河区')
insert into tb values('005' , '003' , '罗湖区')
insert into tb values('006' , '003' , '福田区')
insert into tb values('007' , '003' , '宝安区')
insert into tb values('008' , '007' , '西乡镇')
insert into tb values('009' , '007' , '龙华镇')
insert into tb values('010' , '007' , '松岗镇')
go--创建用户定义函数
create function f_cid(@id varchar(10)) returns varchar(8000)
as
begin
declare @i int , @ret varchar(8000)
declare @t table(id varchar(10) , pid varchar(10) , level int)
set @i = 1
insert into @t select id , pid , @i from tb where id = @id
while @@rowcount <> 0
begin
set @i = @i + 1
insert into @t select a.id , a.pid , @i from tb a , @t b where a.pid = b.id and b.level = @i - 1
end
select @ret = isnull(@ret , '') + id + ',' from @t
return left(@ret , len(@ret) - 1)
end
go --执行查询
select id , children = isnull(dbo.f_cid(id) , '') from tb group by iddrop table tb
drop function f_cid/*
id children
---- ---------------------------------------
001 001,002,003,004,005,006,007,008,009,010
002 002,004
003 003,005,006,007,008,009,010
004 004
005 005
006 006
007 007,008,009,010
008 008
009 009
010 010(所影响的行数为 10 行)
*//*
标题:SQL SERVER 2005中查询指定节点及其所有子节点的方法(字符串形式显示)
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间:2010-02-02
地点:新疆乌鲁木齐
*/create table tb(id varchar(3) , pid varchar(3) , name nvarchar(10))
insert into tb values('001' , null , N'广东省')
insert into tb values('002' , '001' , N'广州市')
insert into tb values('003' , '001' , N'深圳市')
insert into tb values('004' , '002' , N'天河区')
insert into tb values('005' , '003' , N'罗湖区')
insert into tb values('006' , '003' , N'福田区')
insert into tb values('007' , '003' , N'宝安区')
insert into tb values('008' , '007' , N'西乡镇')
insert into tb values('009' , '007' , N'龙华镇')
insert into tb values('010' , '007' , N'松岗镇')
go;with t as
(
select id , cid = id from tb
union all
select t.id , cid = tb.id
from t join tb on tb.pid = t.cid
)
select id , cid = STUFF((SELECT ',' + rtrim(cid) FROM t WHERE id = tb.id FOR XML PATH('')) , 1 , 1 , '')
from tb
group by id
order by id
/*
id cid
---- ---------------------------------------
001 001,002,003,005,006,007,008,009,010,004
002 002,004
003 003,005,006,007,008,009,010
004 004
005 005
006 006
007 007,008,009,010
008 008
009 009
010 010(10 行受影响)
*/;with t as
(
select id , name , cid = id , path = cast(name as nvarchar(100)) from tb
union all
select t.id , t.name , cid = tb.id , path = cast(tb.name as nvarchar(100))
from t join tb on tb.pid = t.cid
)
select id , name ,
cid = STUFF((SELECT ',' + rtrim(cid) FROM t WHERE id = tb.id FOR XML PATH('')) , 1 , 1 , ''),
path = STUFF((SELECT ',' + path FROM t WHERE id = tb.id FOR XML PATH('')) , 1 , 1 , '')
from tb
group by id , name
order by id
/*
id name cid path
---- ---------- ------------------------------------------- ---------------------------------------------------------------------
001 广东省 001,002,003,005,006,007,008,009,010,004 广东省,广州市,深圳市,罗湖区,福田区,宝安区,西乡镇,龙华镇,松岗镇,天河区
002 广州市 002,004 广州市,天河区
003 深圳市 003,005,006,007,008,009,010 深圳市,罗湖区,福田区,宝安区,西乡镇,龙华镇,松岗镇
004 天河区 004 天河区
005 罗湖区 005 罗湖区
006 福田区 006 福田区
007 宝安区 007,008,009,010 宝安区,西乡镇,龙华镇,松岗镇
008 西乡镇 008 西乡镇
009 龙华镇 009 龙华镇
010 松岗镇 010 松岗镇(10 行受影响)
*/drop table tb
-- 获取子节点的根节点,并将结果保存到 ## 全局临时表
;with t as(
select parent,child,parent root from tb where parent not in (select child from tb)
union all
select tb.parent,tb.child,t.root from tb,t where tb.parent=t.child
)
select root,child,
'child'+ltrim(row_number() over (partition by root order by child)) col
into ##
from t;-- 动态行转列
declare @sql varchar(8000),@col varchar(1000);
select @col=isnull(@col,'')+',['+col+']' from ## group by col;
set @sql=
'select root'+@col+char(10)+
'from ## '+char(10)+
'pivot (max(child) for col in('+stuff(@col,1,1,'')+')) pvt';
exec(@sql);
我用上面给的代码执行一点错误都没有,
但是,我一把表换成实际的表执行到
declare @sql varchar(8000),@col varchar(1000);
select @col=isnull(@col,'')+',['+col+']' from ## group by col;
set @sql=
'select root'+@col+char(10)+
'from ## '+char(10)+
'pivot (max(child) for col in('+stuff(@col,1,1,'')+')) pvt';
exec(@sql);
就出现如下的错误:
消息 105,级别 15,状态 1,第 3 行
字符串 'chi)) pvt' 后的引号不完整。
消息 102,级别 15,状态 1,第 3 行
'chi)) pvt' 附近有语法错误。
我昨天研究了一下午没研究个头绪来!
还再请大哥帮忙分析一下!谢谢!
将 @sql 中拼接的 sql 语句显示出来看看。