首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

求SQL语句,取某物料的上层所有物料

2012-12-15 
求SQL语句,取某物料的下层所有物料本帖最后由 laty_cathy 于 2012-07-30 16:25:09 编辑大家好!如何用SQL语

求SQL语句,取某物料的下层所有物料
本帖最后由 laty_cathy 于 2012-07-30 16:25:09 编辑 大家好!
  如何用SQL语句,当标志为'Y'时,将其下层所有物料全部取出来,见下图:

将黄色背景的数据取出来。

谢谢!
[最优解释]

select * from tb as a
(
select * from tb where 标识='Y' 
) as b
where and a.序号 like b.序号+'%'


非技术版 10分
[其他解释]
CREATE TABLE test([阶层] int,[序号] varchar(50),[标志] varchar(2))
go
insert into test
select 1,'0010','' union all
select 2,'0010010','' union all
select 3,'0010010010','' union all
select 2,'00100020','Y' union all
select 3,'001000200010','' union all
select 4,'0010002000100010','' union all
select 5,'00100020001000100010','' union all
select 2,'00100030','' union all
select 3,'001000300010','' union all
select 4,'0010003000100010','Y' union all
select 5,'00100030001000100010',''

SELECT * FROM test as a,(SELECT [序号] FROM test WHERE [标志]='Y') as b
WHERE a.[序号] LIKE b.[序号]+'%' and a.[标志]<>'Y'

drop table test

[其他解释]
模糊查询LIKE应该可以
[其他解释]
数据量不算太大的话,可以用下面的方式:

SELECT * FROM TBL WHERE  序号 LIKE '001000200010%'

[其他解释]
iamaitman 你好!
  我之前有用过游标结合  LIKE 可以实现这样的功能,因为数据量大,占用了好多的时候。想请问高手,有没有好的方法,只用一条SQL语句就可以实现呢!
[其他解释]
引用:
select * from tb as a
(
select * from tb where 标识='Y'
) as b
where and a.序号 like b.序号+'%'


非技术版 10分

[其他解释]
筱筱澄
  您好!
  用你写的SQL,会报错。能帮我检查一下吗?
select * from TEST1 as a ( select * from TEST1 where mk3='Y' ) as b where and a.zyc like b.zyc+'%' 
[其他解释]


IF OBJECT_ID('XX') IS NOT NULL
  DROP TABLE XX
GO

CREATE TABLE XX([阶层] int,[序号] varchar(50),[标志] varchar(2))
go
insert into XX
select 1,'0010','' union all
select 2,'0010010','' union all
select 3,'0010010010','' union all
select 2,'00100020','Y' union all
select 3,'001000200010','' union all
select 4,'0010002000100010','' union all
select 5,'00100020001000100010','' union all
select 2,'00100030','' union all
select 3,'001000300010','' union all
select 4,'0010003000100010','Y' union all
select 5,'00100030001000100010',''



SELECT * FROM XX A
WHERE EXISTS 
(SELECT TOP 1 1 FROM XX B WHERE B.标志 = 'Y' AND A.序号 LIKE(B.序号+'%') AND LEN(A.序号)>LEN(B.序号))



[其他解释]
create table #tb (阶层 int ,序号 varchar(50),标志 char(2))
insert into #tb values(1,'0010','')
insert into #tb values(2,'00100010','')
insert into #tb values(3,'001000100010','')
insert into #tb values(2,'00100020','Y')
insert into #tb values(3,'001000200010','' )--
insert into #tb values(4,'0010002000100010' ,'')--
insert into #tb values(5,'00100020001000100010' ,'')--

insert into #tb values(2,'00100030','')
insert into #tb values(2,'001000300010','')
insert into #tb values(2,'0010003000100010','Y')
insert into #tb values(2,'00100030001000100010','')--
 

select  t.*  
from #tb t ,( select   序号  from #tb   where  标志='Y' )b 
where 
 t.标志<>'Y'
 and 
 substring(t.序号,0,len(b.序号)+1) in(b.序号)

 

[其他解释]
 
create table #tb (阶层 int ,序号 varchar(50),标志 char(2))
insert into #tb values(1,'0010','')
insert into #tb values(2,'00100010','')
insert into #tb values(3,'001000100010','')
insert into #tb values(2,'00100020','Y')
insert into #tb values(3,'001000200010','' )--
insert into #tb values(4,'0010002000100010' ,'')--
insert into #tb values(5,'00100020001000100010' ,'')--

insert into #tb values(2,'00100030','')
insert into #tb values(2,'001000300010','')
insert into #tb values(2,'0010003000100010','Y')
insert into #tb values(2,'00100030001000100010','')--
 

select  t.*  
from #tb t ,( select   序号  from #tb   where  标志='Y' )b 
where 
 t.标志<>'Y'
 and 
 substring(t.序号,0,len(b.序号)+1) in(b.序号)

 /*阶层          序号                                                 标志
----------- -------------------------------------------------- ----
3           001000200010                                         
4           0010002000100010                                     
5           00100020001000100010                                 


2           00100030001000100010                                 

(4 行受影响)*/


[其他解释]
CREATE TABLE test([阶层] int,[序号] varchar(50),[标志] varchar(2))
go
insert into test
select 1,'0010','' union all
select 2,'0010010','' union all
select 3,'0010010010','' union all
select 2,'00100020','Y' union all
select 3,'001000200010','' union all
select 4,'0010002000100010','' union all
select 5,'00100020001000100010','' union all
select 2,'00100030','' union all
select 3,'001000300010','' union all
select 4,'0010003000100010','Y' union all
select 5,'00100030001000100010',''

select * from test

select a.* from test a ,
  (select 序号,LEN(序号) CD from test where 标志 = 'Y') b
  where b.序号 = LEFT(a.序号,b.CD) 
    and a.标志 <> 'Y'

阶层          序号                                                 标志
----------- -------------------------------------------------- ----
3           001000200010                                       
4           0010002000100010                                   
5           00100020001000100010                               
5           00100030001000100010                               

(4 行受影响)

热点排行