我正在尝试将此列表解析为示例 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]
但无法找到任何帮助。
试试这个:
我假设 00 值仅出现在分类的末尾并且可以忽略它们(即它们是 null 的占位符,或类似的东西)。查询如何工作的解释在代码的注释中。无论分类中的图形条目是否按 sno 排序,这都将起作用。
我不确定是否理解所需的结果,但也许 CROSS APPLY 可以解决您的问题。
dbfiddle在这里