我有水果及其子类型的表格。我需要从该表中的数据创建一个sql VIEW 作为分层树。
所以我需要的输出看起来像这样。
我当前的查询以单列形式提供输出。但我需要在单独的列中显示输出,如上图所示。
我的查询:SQL fiddle
WITH items AS (
SELECT FRUIT_ID, FRUIT_NAME
, 0 AS Level
, CAST(FRUIT_NAME AS VARCHAR(255)) AS Path
FROM FRUIT
WHERE PARENT_FRUIT = '0' AND FRUIT_ID = 'FR01'
UNION ALL
SELECT i.FRUIT_ID, i.FRUIT_NAME
, Level + 1
, CAST(Path + ' => ' + CAST(i.FRUIT_NAME AS VARCHAR(255)) AS VARCHAR(255))
FROM FRUIT i
INNER JOIN items itms ON itms.FRUIT_ID = i.PARENT_FRUIT
)
SELECT * FROM items ORDER BY Path
我正在使用Microsoft SQL Server 2014 - 12.0.2000.8(X64)