AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 190221
Accepted
Biju jose
Biju jose
Asked: 2017-11-07 07:35:58 +0800 CST2017-11-07 07:35:58 +0800 CST 2017-11-07 07:35:58 +0800 CST

将其解析为邻接表

  • 772

我正在尝试将此列表解析为示例 DDL 中的邻接列表。

CREATE TABLE [dbo].[classification](
    [sno] [float] NULL,
    [classification] [nvarchar](255) NULL,
    [classificationTitle] [nvarchar](255) NULL
) ON [PRIMARY]

INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (1, N'23-11 00 00', N'Site Products')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (2, N'23-11 11 00', N'Ground Anchorages')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (3, N'23-11 11 11', N'Retaining Stabilizing Ground Anchors')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (4, N'23-11 11 11 11', N'Retaining Stabilizing Ground Components')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (5, N'23-11 11 11 11 11', N'Stabilizing Ground Anchor Heads')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (6, N'23-11 11 11 11 13', N'Stabilizing Ground Tendons')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (7, N'23-11 11 11 13', N'Stabilizing Ground Grouted Anchors')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (8, N'23-11 11 11 15', N'Stabilizing Ground Plate Anchors')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (9, N'23-11 11 11 17', N'Stabilizing Ground Rock Bolts')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (10, N'23-11 11 11 19', N'Stabilizing Ground Rock Anchors')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (11, N'23-11 11 11 21', N'Stabilizing Ground Anchor Tiebacks')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (12, N'23-11 11 13', N'Earth Reinforcement Anchors')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (13, N'23-11 11 13 11', N'Earth Reinforcement Soil Nails')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (14, N'23-11 13 00', N'Ground Improvement Products')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (15, N'23-11 13 11', N'Soil Stabilization Products')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (16, N'23-11 13 11 11', N'Soil Stabilization Injectable Chemicals')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (17, N'23-11 13 11 13', N'Soil Stabilization Pressure Grouting')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (18, N'23-11 13 11 15', N'Ground Freezing Soil Stabilization')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (19, N'23-11 13 11 17', N'Soil Stabilization Fills')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (20, N'23-11 13 11 17 11', N'Soil Stabilization Fill Blocks')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (21, N'23-11 13 11 17 13', N'Soil Stabilization Compressible Fill')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (22, N'23-11 13 11 19', N'Other Soil Stabilizations')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (23, N'23-11 13 11 21', N'Piped Field Drainage')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (24, N'23-11 13 11 21 11', N'Field Drainage Land Drainage Pipes')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (25, N'23-11 13 11 23', N'Field Drainage Blocks')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (26, N'23-11 13 11 25', N'Field Drainage Geocomposite Drains')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (27, N'23-11 13 11 25 11', N'Field Drainage Geocomposite Edge Drains')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (28, N'23-11 13 11 25 13', N'Field Drainage Geocomposite In Place Wall Drains')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (29, N'23-11 13 11 27', N'Geotextile Subsurface Drainage Filtration')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (30, N'23-11 15 00', N'Sheeting and Revetments')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (31, N'23-11 15 11', N'Sheeting Geosynthetics')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (32, N'23-11 15 11 11', N'Sheeting Geotextiles')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (33, N'23-11 15 11 13', N'Sheeting Geogrids')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (34, N'23-11 15 11 15', N'Sheeting Geomembranes')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (35, N'23-11 15 11 17', N'Sheeting Geocomposites')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (36, N'23-11 15 11 19', N'Sheeting Mulch Control Netting')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (37, N'23-11 15 11 21', N'Sheeting Synthetic Erosion Controls')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (38, N'23-11 15 11 23', N'Sheeting Re vegetation Mats')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (39, N'23-11 15 11 25', N'Sheeting Turf Reinforcement Mats')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (40, N'23-11 15 13', N'Revetments')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (41, N'23-11 15 13 11', N'Revetment Soil Blankets')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (42, N'23-11 15 13 13', N'Pool Revetments')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (43, N'23-11 15 13 15', N'Trench Revetments')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (44, N'23-11 15 13 17', N'Revetment Rock Linings')
GO
INSERT [dbo].[classification] ([sno], [classification], [classificationTitle]) VALUES (45, N'23-11 15 13 19', N'Revetment Ripraps')
GO

我正在尝试获得此输出

+----------+---------+
| parentid | childid |
+----------+---------+
| NULL     |       1 |
| 1        |       2 |
| 2        |       3 |
| 3        |       4 |
| 4        |       5 |
| 4        |       6 |
| 3        |       7 |
| 3        |       8 |
| 3        |       9 |
| 3        |      10 |
| 3        |      11 |
| 1        |      12 |
| 12       |      13 |
+----------+---------+

到目前为止,我已经想出了这个。

;with cte as (
select LEFT([classification], LEN([classification]) - 3)as immediateparent,
[classification],sno
FROM [classification]
)
select a.sno,cte.sno from [classification] a 
left join cte on cte.immediateparent =a.[classification]

但无法找到任何帮助。

sql-server t-sql
  • 2 2 个回答
  • 57 Views

2 个回答

  • Voted
  1. Best Answer
    mathewb
    2017-11-07T09:56:45+08:002017-11-07T09:56:45+08:00

    试试这个:

    SELECT  parent.sno parentid, child.sno childid
    FROM    classification child
    --get all the matches where parent.classification is a subset of child.classification
    OUTER APPLY (
        --only consider the highest ranking result
        SELECT  TOP 1 *
        FROM    classification parent
        --ignore the 00 entries - these appear to represent null values
        WHERE   CHARINDEX(REPLACE(parent.classification, ' 00', ''), child.classification) > 0
        --don't consider equivalent records as a match
        AND parent.sno <> child.sno
        --where the rank is based on the length of the match (assume the longest match is the adjacent node
        ORDER BY LEN(REPLACE(parent.classification, ' 00', '')) DESC
    ) parent
    --based on the results displayed in the question
    WHERE   child.sno BETWEEN 1 AND 13
    --based on the ordering displayed in the question
    ORDER BY child.sno, parent.sno
    

    我假设 00 值仅出现在分类的末尾并且可以忽略它们(即它们是 null 的占位符,或类似的东西)。查询如何工作的解释在代码的注释中。无论分类中的图形条目是否按 sno 排序,这都将起作用。

    • 1
  2. McNets
    2017-11-07T08:40:06+08:002017-11-07T08:40:06+08:00

    我不确定是否理解所需的结果,但也许 CROSS APPLY 可以解决您的问题。

    SELECT c1.sno, c1.classification, c2.nextClass, c2.nextSno
    FROM   Classification c1
    CROSS APPLY (SELECT TOP 1 classification nextClass, sno nextSno
                 FROM   classification
                 WHERE  classification > c1.classification
                 ORDER BY classification) c2
    ORDER BY classification;
    GO
    
    斯诺 | 分类 | 下一班 | 下一个Sno
    --: | :---------------- | :---------------- | ------:
      1 | 23-11 00 00 | 23-11 11 00 | 2个
      2 | 23-11 11 00 | 23-11 11 11 | 3个
      3 | 23-11 11 11 | 23-11 11 11 11 | 4个
      4 | 23-11 11 11 11 | 23-11 11 11 11 11 | 5个
      5 | 23-11 11 11 11 11 | 23-11 11 11 11 13 | 6个
      6 | 23-11 11 11 11 13 | 23-11 11 11 13 | 7
      7 | 23-11 11 11 13 | 23-11 11 11 15 | 8个
      8 | 23-11 11 11 15 | 23-11 11 11 17 | 9
      9 | 23-11 11 11 17 | 23-11 11 11 19 | 10
     10 | 23-11 11 11 19 | 23-11 11 11 21 | 11
     11 | 23-11 11 11 21 | 23-11 11 13 | 12
     12 | 23-11 11 13 | 23-11 11 13 11 | 13
     13 | 23-11 11 13 11 | 23-11 13 00 | 14
     14 | 23-11 13 00 | 23-11 13 11 | 15
     15 | 23-11 13 11 | 23-11 13 11 11 | 16
     16 | 23-11 13 11 11 | 23-11 13 11 13 | 17
     17 | 23-11 13 11 13 | 23-11 13 11 15 | 18
     18 | 23-11 13 11 15 | 23-11 13 11 17 | 19
     19 | 23-11 13 11 17 | 23-11 13 11 17 11 | 20
     20 | 23-11 13 11 17 11 | 23-11 13 11 17 13 | 21
     21 | 23-11 13 11 17 13 | 23-11 13 11 19 | 22
     22 | 23-11 13 11 19 | 23-11 13 11 21 | 23
     23 | 23-11 13 11 21 | 23-11 13 11 21 11 | 24
     24 | 23-11 13 11 21 11 | 23-11 13 11 23 | 25
     25 | 23-11 13 11 23 | 23-11 13 11 25 | 26
     26 | 23-11 13 11 25 | 23-11 13 11 25 11 | 27
     27 | 23-11 13 11 25 11 | 23-11 13 11 25 13 | 28
     28 | 23-11 13 11 25 13 | 23-11 13 11 27 | 29
     29 | 23-11 13 11 27 | 23-11 15 00 | 30
     30 | 23-11 15 00 | 23-11 15 11 | 31
     31 | 23-11 15 11 | 23-11 15 11 11 | 32
     32 | 23-11 15 11 11 | 23-11 15 11 13 | 33
     33 | 23-11 15 11 13 | 23-11 15 11 15 | 34
     34 | 23-11 15 11 15 | 23-11 15 11 17 | 35
     35 | 23-11 15 11 17 | 23-11 15 11 19 | 36
     36 | 23-11 15 11 19 | 23-11 15 11 21 | 37
     37 | 23-11 15 11 21 | 23-11 15 11 23 | 38
     38 | 23-11 15 11 23 | 23-11 15 11 25 | 39
     39 | 23-11 15 11 25 | 23-11 15 13 | 40
     40 | 23-11 15 13 | 23-11 15 13 11 | 41
     41 | 23-11 15 13 11 | 23-11 15 13 13 | 42
     42 | 23-11 15 13 13 | 23-11 15 13 15 | 43
     43 | 23-11 15 13 15 | 23-11 15 13 17 | 44
     44 | 23-11 15 13 17 | 23-11 15 13 19 | 45
    

    dbfiddle在这里

    • 0

相关问题

  • SQL Server - 使用聚集索引时如何存储数据页

  • 我需要为每种类型的查询使用单独的索引,还是一个多列索引可以工作?

  • 什么时候应该使用唯一约束而不是唯一索引?

  • 死锁的主要原因是什么,可以预防吗?

  • 如何确定是否需要或需要索引

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve