0

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

                     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  

因此,我需要一个能返回准确结果的查询。我可以在查询中使用的唯一参数是“类型”,并且始终为“1”。

PS:我使用的是 SQL Server 2008。

4

1 回答 1

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
于 2013-07-03T09:15:30.197 回答