SQL选择倒排树

发布于 2024-08-20 18:29:41 字数 3899 浏览 4 评论 0原文

你好,我在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.

等等。

非逆向选择看起来像这样(我使用 SqlServer 2008):

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

Hello i have tree structure in sql. Logics is standard: SomeID, ParentID, other fields.
I have select procedure, which selects data like this:

1.
1.1
1.1.1

and so on.

How to write the select procedure, to get the inverted result(first are selected the deepest branches, last - root branches), like this:

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

and so on.

Non inversive select looks like thi(I use 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦晓ヶ微光ヅ倾城 2024-08-27 18:29:41

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

If you can't do recursion then I can only think of one other solution. I am sure it is not optimal but. You could do what you are doing above and insert that data into a temp table with 2 extra columns. The one column would hold your parent ID as it seems you are still sorting in descending order on the highest level (since you have all of the 1's before all of the 2's) and the other could just hold a seeded Identity integer. Then you could just query the table and sort on the original parent ID (the very first number) in ascending order and then the seeded Identity integer in descending order. From what I gather that would work but it would be inefficient.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文