0
我想创建一个视图,它将返回一个3级数据的字符串。sql - 显示祖父母,父母和孩子
select bf.functionName + ' \ ' + ISNULL(bf1.functionName + ' \ ', '') + ISNULL(bf2.functionName, '') from tblBusinessFunction bf
inner join tblBusinessFunction bf1 on bf1.parentID = bf.id and bf.level = 0
inner join tblBusinessFunction bf2 on bf2.parentID = bf1.id and bf1.level = 1
以上只是返回顶级祖父母和父母,而不是子级别。
这就是表看起来像
| id | functionName | parentID | level |
|:-----|----------------------------:|:--------:|:-------:|
| 101 | Portfolio Strategy Functions| NULL | 0 |
| 110 | Research | 101 | 1 |
| 111 | Economic Forecasting | 110 | 2 |
现在我的查询将返回Portfolio Strategy Functions \ Research \ Economic Forecasting
,但我希望它也返回Portfolio Strategy Functions \ Research
它不会做。
这是一个伟大的地方开始。 http://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/ –
切换到“左连接”。并看看** recoursive CTE **。 –
我在示例数据中看不到任何名为'level'的字段。 –