From your question and a check on ORACLE's syntax, you appear to need a recursive query that has a single anchor member and then recurses through based on the relationship between PARENTID
and FILEID
.
SQL Server's T-SQL syntax approached this differently. It uses a CTE expression to effectively add the recursive part with the anchor member.
I have assumed that the fetch first row only
statement restricts it to a single root, but as you haven't provided sample data I don't know if it is safe to assume there will only be a single row returned by the anchor component. I have therefore used a TOP
statement to enforce this.
Without sample data, I haven't tested the query, but it should be used as a starting point and refined as required. You can find more about recursive CTEs at this Docs page
SAMPLE QUERY
WITH ParentQuery ()
AS (
SELECT TOP (1)
FT.[FILE_ID],
FT.REVISION,
FT.REVISION_TIME,
FT.[NAMESPACE],
FT.FULL_PATH,
FT.OWNED_BY_NAME,
FT.OWNED_BY_TYPE,
FT.EXT_ATTRIBUTES,
FT.SYS_CREATED_BY_NAME,
FT.SYS_CREATED_BY_TYPE,
FT.SYS_MODIFIED_BY_NAME,
FT.SYS_MODIFIED_BY_TYPE,
FT.ACL_ID,
0 as [RANK]
FROM BI180123.CSS_SI_FILES FT
WHERE UPPER(convert(varchar, FT.FULL_PATH))= UPPER('/shared')
AND FT.NAMESPACE= 'demo'
UNION ALL
SELECT
F1.[FILE_ID],
F1.REVISION,
F1.REVISION_TIME,
F1.[NAMESPACE],
F1.FULL_PATH,
F1.OWNED_BY_NAME,
F1.OWNED_BY_TYPE,
F1.EXT_ATTRIBUTES,
F1.SYS_CREATED_BY_NAME,
F1.SYS_CREATED_BY_TYPE,
F1.SYS_MODIFIED_BY_NAME,
F1.SYS_MODIFIED_BY_TYPE,
F1.ACL_ID,
ROW_NUMBER() OVER (
PARTITION BY F1.LOOKUP_KEY, F1.PATH_DEPTH
ORDER BY F1.REVISION_TIME DESC
)
FROM BI180123.CSS_SI_FILES F1
INNER JOIN ParentQuery pq ON pq.[FILE_ID] = F1.PARENT_ID
WHERE F1.SERVICE_INSTANCE_ID = 1
AND F1.[NAMESPACE] = 'demo'
)
SELECT [FILE_ID],
REVISION,
REVISION_TIME,
[NAMESPACE],
FULL_PATH,
OWNED_BY_NAME,
OWNED_BY_TYPE,
EXT_ATTRIBUTES,
SYS_CREATED_BY_NAME,
SYS_CREATED_BY_TYPE,
SYS_MODIFIED_BY_NAME,
SYS_MODIFIED_BY_TYPE,
ACL_ID,
[RANK]
FROM ParentQuery
ORDER BY [FILE_ID] ;