SQL选择倒置树

时间:2010-02-10 14:29:12

标签: sql-server-2008 recursive-query

你好我在sql中有树结构。逻辑是标准的:SomeID,ParentID,其他字段。 我有选择程序,它选择这样的数据:

1.
1.1
1.1.1

等等。

如何编写选择过程,得到反转结果(首先选择最深分支,最后根分支),如下所示:

1.1.1.
1.1.
1.
2.2.2.2.2.
2.2.2.2.
2.2.2.
2.2.
2.

等等。

非反向选择看起来像thi(我使用SqlServer 2008)s:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[Object_SelectDownByRoot]
@ObjectID int

AS
WITH tree (ObjectID, ParentID, ObjectName, ObjectCode, DistrictID, DistrictName, 
                      CityName, RegionName, StreetName, StreetID, AddressID, ObjectTypeName, 
                      RouteName, ObjectTypeID, RouteID, AvrgTempIn, Area, Volume, 
                      ElectricPower, ObjectStatusName, ObjectStatusID, [ControlRoom?], DateBuild, 
                      [Floor], EncloseName, EncloseID, MaintenanceEval, AdministratorID, 
                      Administrator, ElectricityPerson, ElectricityPersonID, 
                      HeatingPersonID, HeatingPerson, HouseNo, FlatNo, ZIP, 
                      AddressStreet, RouteCode, RouteDescription, 
                      AddressDescription, StreetID2, CityID, AddressCityName) AS
    (
        SELECT  
        ObjectID, ParentID, ObjectName, ObjectCode, DistrictID, DistrictName, 
                          CityName, RegionName, StreetName, StreetID, AddressID, ObjectTypeName, 
                          RouteName, ObjectTypeID, RouteID, AvrgTempIn, Area, Volume, 
                          ElectricPower, ObjectStatusName, ObjectStatusID, [ControlRoom?], DateBuild, 
                          [Floor], EncloseName, EncloseID, MaintenanceEval, AdministratorID, 
                          Administrator, ElectricityPerson, ElectricityPersonID, 
                          HeatingPersonID, HeatingPerson, HouseNo, FlatNo, ZIP, 
                          AddressStreet, RouteCode, RouteDescription, 
                          AddressDescription, StreetID2, CityID, AddressCityName
         FROM dbo.[ObjectQ] ofs
         WHERE( ObjectID = @ObjectID )

         UNION ALL

         SELECT     ofs.ObjectID, ofs.ParentID, ofs.ObjectName, ofs.ObjectCode, ofs.DistrictID, ofs.DistrictName, 
                          ofs.CityName, ofs.RegionName, ofs.StreetName, ofs.StreetID, ofs.AddressID, ofs.ObjectTypeName, 
                          ofs.RouteName, ofs.ObjectTypeID, ofs.RouteID, ofs.AvrgTempIn, ofs.Area, ofs.Volume, 
                          ofs.ElectricPower, ofs.ObjectStatusName, ofs.ObjectStatusID, ofs.[ControlRoom?], ofs.DateBuild, 
                          ofs.[Floor], ofs.EncloseName, ofs.EncloseID, ofs.MaintenanceEval, ofs.AdministratorID, 
                          ofs.Administrator, ofs.ElectricityPerson, ofs.ElectricityPersonID, 
                          ofs.HeatingPersonID, ofs.HeatingPerson, ofs.HouseNo, ofs.FlatNo, ofs.ZIP, 
                          ofs.AddressStreet, ofs.RouteCode, ofs.RouteDescription, 
                          ofs.AddressDescription, ofs.StreetID2, ofs.CityID, ofs.AddressCityName
          FROM dbo.[ObjectQ] ofs
          JOIN tree ON tree.ObjectID = ofs.ParentID
    )

    SELECT  
    ObjectID, ParentID, ObjectName, ObjectCode, DistrictID, DistrictName, 
                      CityName, RegionName, StreetName, StreetID, AddressID, ObjectTypeName, 
                      RouteName, ObjectTypeID, RouteID, AvrgTempIn, Area, Volume, 
                      ElectricPower, ObjectStatusName, ObjectStatusID, [ControlRoom?], DateBuild, 
                      [Floor], EncloseName, EncloseID, MaintenanceEval, AdministratorID, 
                      Administrator, ElectricityPerson, ElectricityPersonID, 
                      HeatingPersonID, HeatingPerson, HouseNo, FlatNo, ZIP, 
                      AddressStreet, RouteCode, RouteDescription, 
                      AddressDescription, StreetID2, CityID, AddressCityName
    FROM tree

1 个答案:

答案 0 :(得分:1)

如果你不能做递归,那么我只能想到另一个解决方案。我确信它不是最佳的但是。您可以执行上面所做的操作,并将该数据插入到具有2个额外列的临时表中。一列将保存您的父ID,因为它似乎仍然在最高级别按降序排序(因为您在所有2之前都拥有所有1),而另一列可能只保留种子身份整数。然后你可以查询表并按升序对原始父ID(第一个数字)进行排序,然后按降序对种子身份整数进行排序。从我收集的内容中可行但效率低下。