SQL-Query根据特定条件从单个表中获取第一级子详细信息

时间:2013-07-03 08:59:23

标签: sql sql-server sql-server-2008 tsql

我的数据库中的表包含如下数据,

                     TBLlocations
 -------------------------------------------------------
 LocationId    LocationName      RegisteredUnder    Type
 --------------------------------------------------------
  LOC100        Location1            0               0
  LOC201        Location2           LOC100           2
  LOC102        Location3           LOC201           1
  LOC302        Location4           LOC201           1
  LOC103        Location5           LOC201           1
  LOC104        Location6           LOC201           1
  LOC105        Location7           LOC104           1
  LOC106        Location8           LOC105           1
  LOC107        Location9           LOC106           1

现在我必须从上表中选择位置,以便我的查询将返回第一级位置,即;考虑到上面的表,我的查询必须返回位置 其类型为“1”,应该是类型为“1”的第一级子位置。从上表中,位置3到6是第一级位置,因此查询应返回以下内容:

---------------
   Location3 
   Location4 
   Location5 
   Location6

我尝试加入同一个表格,为“类型”提供条件。 这是我建立的查询:

Select Distinct t1.LocationId,t1.LocationName,t1.RegisteredUnder from TBLlocations t1
join TBLlocations t2 on t2.RegisteredUnder!=t1.LocationId
where t1.Type='1' and t2.Type='1'
order by t1.RegisteredUnder

上述查询返回了类型“1”下的所有位置,如下所示:

--------------------------------------------------
LocationId     LocationName        RegisteredUnder
--------------------------------------------------
  LOC102        Location3           LOC201 
  LOC302        Location4           LOC201
  LOC103        Location5           LOC201
  LOC104        Location6           LOC201
  LOC105        Location7           LOC104 
  LOC106        Location8           LOC105
  LOC107        Location9           LOC106  

因此,我需要一个可以返回确切结果的查询。我可以在查询中使用的唯一参数是'Type',它始终是'1'。

PS:我正在使用SQL Server 2008。

1 个答案:

答案 0 :(得分:1)

问题改变后

Declare @a table (LocationId Varchar(100),   LocationName Varchar(100),     RegisteredUnder Varchar(100),   Type int)
Insert into @a  Values('LOC100','Location1','0',0)
Insert into @a  Values('LOC201','Location2','LOC100',2)
Insert into @a  Values('LOC102','Location3','LOC201',1)
Insert into @a  Values('LOC302','Location4','LOC201',1)
Insert into @a  Values('LOC103','Location5','LOC201',1)
Insert into @a  Values('LOC104','Location6','LOC201',1)
Insert into @a  Values('LOC105','Location7','LOC104',1)
Insert into @a  Values('LOC106','Location8','LOC105',1)
Insert into @a  Values('LOC107','Location9','LOC106',1)
;With CTE as
(
Select 0 as level,* from @a where Type=1
UNION ALL
Select   c.Level+1, a.* from @a a
join CTE c on c.LocationId=a.RegisteredUnder and a.Type=1
)     
Select  c1.* from CTE c1
Left Join CTE c2 on c2.LocationId=c1.LocationId and c2.level>0
where c2.LocationId is NULL
order by LEVEL desc,LocationName

问题之前的答案已经改变

Declare @a table (LocationId Varchar(100),   LocationName Varchar(100),     RegisteredUnder Varchar(100),   Type int)
Insert into @a  Values('LOC100','Location1','0',0)
Insert into @a  Values('LOC201','Location2','LOC100',2)
Insert into @a  Values('LOC102','Location3','LOC201',1)
Insert into @a  Values('LOC302','Location4','LOC201',1)
Insert into @a  Values('LOC103','Location5','LOC201',1)
Insert into @a  Values('LOC104','Location6','LOC201',1)
Insert into @a  Values('LOC105','Location7','LOC104',1)
Insert into @a  Values('LOC106','Location8','LOC105',1)
Insert into @a  Values('LOC107','Location9','LOC106',1)
;With CTE as
(
Select 0 as level,* from @a where RegisteredUnder='LOC201'
UNION ALL
Select   c.Level+1, a.* from @a a
join CTE c on c.RegisteredUnder=a.LocationId
) 
Select DISTINCT * from CTE
where level<2
order by LEVEL desc, LocationName