Declare @ Table
(BillNo varchar(10),
sParent varchar(10)
)Insert Into @
Select 'D001', '' Union
Select 'B001', 'D001' union
Select 'B002', 'D001' union
Select 'X001', 'B001' union
Select 'X002', 'B002' select a.sparent d,a.billno b,b.billno x
from @ a left join @ b
on a.BillNo=b.sParent
where a.sparent='D001'
解决方案 »
- 考试题,不会做,请教一个SQL
- MSSQL2000 FillAddress(MSAFD Tcpip [TCP/IPv6])
- 很怪异的事件,明明没有对用户赋权,为什么就能访问呢表呢
- 清空表,bcp导入数据,数据混乱问题
- vs2012开发的winform程序,数据库是sql2008r2的如何打包
- 如何将sql中的查询分析器中生成的数据转换成为其他的格式。比如excel等
- ~~~~~~~~~请大家帮帮小弟,过来看看,马上给分~~~~~~~~
- 两数据库的同步问题?
- 请教sql语言高手!!!高分80分!!
- 怎样获取SQL SERVER 当前是否启动了事务以及事务的数目?
- 枫兮在马?????枫兮在马?????
- 难!怎么屏蔽带标记的记录行?
--建立测试环境Declare @ table
(BillNo varchar(10),
sParent varchar(10)
)Insert Into @
Select 'D001', '' Union
Select 'B001', 'D001' union
Select 'B002', 'D001' union
Select 'X001', 'B001' union
Select 'X002', 'B002' /*
需要结果
d b x
D001 B001 X001
D001 B002 X002
*/select * from @
select a.sparent d,a.billno b,b.billno x
from @ a left join @ b
on a.BillNo=b.sParent
where a.sparent='D001'/*
D001
B001 X001 D001
B002 X002 */
from @ a left join @ b
on a.BillNo=b.sParent
where a.sparent='D001' -- 这条件不能加的,我只是举了个例子,不能具体到某一个单子的!
Declare @ Table
(BillNo varchar(10),
sParent varchar(10)
)Insert Into @
Select 'D001', '' Union
Select 'B001', 'D001' union
Select 'B002', 'D001' union
Select 'X001', 'B001' union
Select 'X002', 'B002' select * from @
/*
需要结果
d b x
D001 B001 X001
D001 B002 X002
*/
declare @t table (billno varchar(10))
insert into @t
select billno from @ where billno like 'd%'
union all
select billno from @ where billno like 'b%'
union all
select billno from @ where billno like 'x%'
select * from @t
select d=(case when billno like 'd%' then billno else '' end),b=(case when billno like 'b%' then billno else '' end),x=(case when billno like 'x%' then billno else '' end) from @t
--
D001
B001
B002
X001
X002
(BillNo varchar(10),
sParent varchar(10)
)Insert Into @
Select 'D001', '' Union
Select 'B001', 'D001' union
Select 'B002', 'D001' union
Select 'X001', 'B001' union
Select 'X002', 'B002'
select y.d,y.b,y.x
from(select a.sparent d,a.billno b,b.billno x
from @ a right join @ b
on a.BillNo=b.sParent)y,@ g
where g.billno=y.d