日期:2014-05-18  浏览次数:20571 次

路過走過,請各位看一看這個問題!!!!!!!!
表1
order       SN1               SN2
11111       75A1001       75A0001
11111       75A1003       75A0002
11111       75A1002       75A0003
22222       75A2001       75A0001
22222       75A2002       75A0002
22222       75A2003       75A0003
33333       75A3001       75A1001
33333       75A3002       75A1002
33333       75A3003       75A1003
.................
如上面,如何得到如下結果:
SN1               SN2                 SN3                 SN4
75A0001       75A1001         75A2001         75A3001
75A0002       75A1002         75A2002         75A3002
75A0003       75A1003         75A2003         75A3003
.................

表1中SN1不會重復,SN2總對應SN1的上一階,如何把相關聯的數據變成一條記錄,如上?
謝謝各位啦!!!

------解决方案--------------------
看不懂

------解决方案--------------------
有点困难
------解决方案--------------------
明是明了..可是,我想到的是用函数..但是...试试好了~
------解决方案--------------------
create function f_getParent(@child varchar(20),@int int)
returns varchar(30)
as
begin
while @int > 0
begin
if not exists(select 1 from t1 where SN1=@child and SN2 <> ' ')
set @child= ' '
else
select @child=SN2 from t2 where SN1=@child
set @int=@int-1
end
return @child
end
go


select [SN1],dept_parent=dbo.f_getParent(SN2,1) SN2,
dept_parent=dbo.f_getParent(SN2,2) SN3,dept_parent=dbo.f_getParent(SN2,3) SN4,
from t1


?????我想到是这样的..不知道有没有更好的方法
------解决方案--------------------
学习中 ~~~
mark~!
------解决方案--------------------
學習中,UP