我目前正在检索具有多个父类别及其子类别的费用类别,依此类推。为此,我使用通用表表达式来应用递归,但无法检索所需的结果。
这是我得到的结果
CId Tit Par
----------------------------------------------------------------
D61D6C22 Electricity Bill NULL
52113021 Wire Bill D61D6C22
E34B6F68 Wireless Bill D61D6C22
41A35796 B category Bill 52113021
D063745A A category Bill 52113021
这是我使用的查询:
WITH cte1 AS
(
SELECT
Id AS [CId], Title as [Tit], ParentId as [Par],
RN = ROW_NUMBER() OVER (ORDER BY Title)
FROM
dbo.[ExpenseCategory]
WHERE
ParentId IS NULL
GROUP BY
Id, Title, ParentId
UNION ALL
SELECT
Id AS [CId], Title, ParentId, RN
FROM
cte1
INNER JOIN
dbo.[ExpenseCategory] ec ON cte1.[CId] = ec.ParentId
)
SELECT
cte1.[CId], [Tit],[Par]
FROM
cte1
ORDER BY
RN ASC
CId Tit Par
----------------------------------------------------------------
D61D6C22 Electricity Bill NULL
52113021 Wire Bill D61D6C22
41A35796 B category Bill 52113021
D063745A A category Bill 52113021
E34B6F68 Wireless Bill D61D6C22