我有一个递归表:
CREATE TABLE [dbo].[CATEGORIE](
[id_categorie] [int] IDENTITY(1,1) NOT NULL,
[Nom] [varchar](50) NOT NULL,
[_Description] [varchar](2048) NULL,
[id_categorie_1] [int] NULL,
[id_codesActivite] [int] NULL,
PRIMARY KEY CLUSTERED
(
[id_categorie] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [dbo].[CATEGORIE] WITH CHECK ADD CONSTRAINT
[FK_CATEGORIE_id_categorie_1] FOREIGN KEY([id_categorie_1])
REFERENCES [dbo].[CATEGORIE] ([id_categorie])
GO
我想查询一个结果,该表的所有子项都在不同的列中,如下所示:
COL1 | COL2 | COL3
----------------------------
CAT1 | |
CAT1 | SCAT1-1 |
CAT1 | SCAT1-1 | SSCAT1-1-1
CAT1 | SCAT1-1 | SSCAT1-1-2
CAT1 | SCAT1-1 | SSCAT1-1-3
CAT1 | SCAT1-2 |
CAT1 | SCAT1-2 | SSCAT1-2-1
CAT1 | SCAT1-2 | SSCAT1-2-2
CAT1 | SCAT1-2 | SSCAT1-2-3
CAT2 | |
CAT2 | SCAT2-1 |
CAT2 | SCAT2-2 |
CAT3 | |
CAT3 | SCAT3-1 |
CAT3 | SCAT3-1 | SSCAT3-1-1
CAT3 | SCAT3-1 | SSCAT3-1-2
我认为我必须使用递归查询,例如此链接 https://sqlpro.developpez.com/cours/sqlserver/cte-recursives/
但是我找不到好的。
谢谢
编辑:
目前我对此代码有看法:
SELECT
(SELECT Nom
FROM dbo.CATEGORIE AS CAT3
WHERE (id_categorie =
(SELECT id_categorie_1
FROM dbo.CATEGORIE AS CAT2
WHERE (id_categorie = dbo.CATEGORIE.id_categorie_1)))) AS CAT1,
(SELECT Nom
FROM dbo.CATEGORIE AS CAT2
WHERE (id_categorie = dbo.CATEGORIE.id_categorie_1)) AS CAT2,
Nom AS CAT3,
id_categorie
FROM dbo.CATEGORIE
可以,但是我需要在C#代码中“移动”列
+------+--------+----------+--------------+
| CAT1 | CAT2 | CAT3 | id_categorie |
+------+--------+----------+--------------+
| NULL | NULL | CAT1 | 392 |
+------+--------+----------+--------------+
| NULL | CAT1 | CAT1-1 | 393 |
+------+--------+----------+--------------+
| NULL | CAT1 | CAT1-2 | 394 |
+------+--------+----------+--------------+
| NULL | CAT1 | CAT1-3 | 395 |
+------+--------+----------+--------------+
| CAT1 | CAT1-1 | CAT1-1-1 | 396 |
+------+--------+----------+--------------+
| CAT1 | CAT1-1 | CAT1-1-2 | 397 |
+------+--------+----------+--------------+
| CAT1 | CAT1-2 | CAT1-2-1 | 398 |
+------+--------+----------+--------------+
| NULL | NULL | CAT2 | 399 |
+------+--------+----------+--------------+
| NULL | NULL | CAT3 | 400 |
+------+--------+----------+--------------+
| NULL | CAT3 | CAT3-1 | 401 |
+------+--------+----------+--------------+
| NULL | CAT3 | CAT3-2 | 402 |
+------+--------+----------+--------------+
| CAT3 | CAT3-2 | CAT3-2-1 | 403 |
+------+--------+----------+--------------+
| CAT3 | CAT3-2 | CAT3-2-2 | 404 |
+------+--------+----------+--------------+
| CAT3 | CAT3-2 | CAT3-2-3 | 405 |
+------+--------+----------+--------------+
答案 0 :(得分:0)
尝试先构建父查询,然后再构建子查询,无论您要构建的深度如何。
例如,如果使用id_categorie_1 ='Parent'检索父对象,则父查询
Select id_categorie AS COL1 FROM CATEGORIE WHERE id_categorie_1='Parent'
对于父母(第一代)的孩子
Select catPARENT.id_categorie AS COL1, ISNULL(catChild1.id_categorie,'') AS COL2
FROM CATEGORIE AS catPARENT LEFT JOIN CATEGORIE AS catChild1
ON catPARENT.id_categorie=catChild1.id_categorie_1
WHERE catPARENT.id_categorie_1='Parent'
问题的答案,孙子(第二代)
Select catPARENT.id_categorie AS COL1, ISNULL(catChild1.id_categorie,'') AS COL2
, ISNULL(catChild2.id_categorie,'') AS COL3
FROM CATEGORIE AS catPARENT LEFT JOIN CATEGORIE AS catChild1
ON catPARENT.id_categorie=catChild1.id_categorie_1 LEFT JOIN CATEGORIE AS catChild2
ON catChild1.id_categorie=catChild2.id_categorie_1
WHERE catPARENT.id_categorie_1='Parent'
答案 1 :(得分:0)
如果只需要三列,则可以执行以下操作:
select c.name, c1.nom, c2.nom
from categorie c left join
categorie c1
on c1.id_categorie_1 = c.id_categorie left join
categorie c2
on c2.id_categorie_1 = c1.id_categorie
where c.id_categorie_1 is null;
这将获得最高级别的类别和接下来的两个级别。对于此示例,无需进行递归查询。
答案 2 :(得分:0)
使用Item i1 i2 i3 i4
User
u1 NaN NaN NaN 2.0
u4 2.0 NaN NaN NaN
u5 NaN 1.0 NaN 3.0
。使用与每个子成员串联的recursive CTE
。这最多支持递归9级。如果需要更多,请使用seq
之类的数字代替01
。还要在select语句中相应地添加列。
1