此CTE获取Private Sub ConcatValues()
Dim MyProperty As String
Dim Fun() As String
Dim i As Integer
Dim Rl As Long
Dim R As Long, C As Long
i = -1
With Worksheets("tbValue")
' find the last used row in column A
Rl = .Cells(.Rows.Count, "A").End(xlUp).Row
For R = 7 To Rl ' start with row 7 (??)
i = i + 1
ReDim Preserve Fun(i)
For C = 16 To 35
Fun(i) = Fun(i) & Chr(34) & .Cells(6, C).Value & Chr(34) & _
":" & Chr(34) & .Cells(R, C).Value & Chr(34) & "; "
Next C
MyProperty = Join(Fun)
Debug.Print MyProperty
Next R
End With
End Sub
s和SuiteID
s的层次结构作为下面的结果集。我希望传递父母ParentSuiteID
和所有SuiteID
的各自级别。
SuiteID
+---------+------------------------+---------------+-------+ | SuiteID | Title | ParentSuiteID | Level | +---------+------------------------+---------------+-------+ | 10664 | root | 0 | 0 | | 10681 | Prod Test Environment | 10664 | 1 | | 11097 | Dev Test Environment | 10664 | 1 | | 11155 | Training Environment | 10664 | 1 | | 11156 | Production Environment | 10664 | 1 | | 11100 | Bridge PMS | 11097 | 2 | | 11126 | Bridge PTS | 11097 | 2 | | 11139 | Client 360 | 11097 | 2 | | 11140 | Contact Manager | 11097 | 2 | | 11145 | Revenue DashBoard | 11097 | 2 | | 11141 | Finance flow | 11140 | 3 | | 11142 | Premium Finance flow | 11140 | 3 | | 11143 | Client Contacts | 11140 | 3 | | 11127 | Direct Bill | 11126 | 3 | +---------+------------------------+---------------+-------+
我想写一个查询,当我通过(SuiteID = 11100)时给出以下结果集:
SuiteID Title ParentSuiteID Level 10664 root 0 0 11097 Dev Test Environment 10664 1 11100 Bridge PMS 11097 2
答案 0 :(得分:1)
Declare @Table table (SuiteID int,Title varchar(50),ParentSuiteID int)
Insert into @Table values
(10664 ,'root ', 0),
(10681 ,'Prod Test Environment ',10664 ),
(11097 ,'Dev Test Environment ',10664 ),
(11155 ,'Training Environment ',10664 ),
(11156 ,'Production Environment ',10664 ),
(11100 ,'Bridge PMS' ,11097 )
Declare @Fetch int = 11100
;with cteHB as (
Select SuiteID
,ParentSuiteID
,Lvl=1
,Title
From @Table
Where SuiteID =@Fetch
Union All
Select R.SuiteID
,R.ParentSuiteID
,P.Lvl+1
,R.Title
From @Table R
Join cteHB P on P.ParentSuiteID = R.SuiteID )
Select Lvl = Row_Number() over (Order By Lvl Desc) -1
,SuiteID
,ParentSuiteID
,Title
From cteHB
Order By 1
返回
Lvl SuiteID ParentSuiteID Title
0 10664 0 root
1 11097 10664 Dev Test Environment
2 11100 11097 Bridge PMS