【问题标题】:SQL Server Graph Tables - navigate through certain nodes to find endSQL Server Graph Tables - 浏览某些节点以找到结束
【发布时间】:2021-03-06 01:30:16
【问题描述】:

我有一个包含 2 个节点表和 1 个边缘表的图表结构。

Symptom 表是 Node 表,Syndrome_Causes 是 Edge 表。此外,还有一个名为 Syndrome 的节点表。

我的目标是有一个查询,我可以在其中遍历我的图,通过特定的节点。

我已经编写了下面的查询来显示所有可能的路线,从“心血管”节点开始,但我需要过滤掉通过路径的结果,在下图中用红色下划线(心血管->心悸->持续时间->间隔->天)。

select distinct
    s1.symptom_name
    , string_agg(s2.symptom_name, '->') within group (graph path) AS links
    , last_value(s2.symptom_name) within group (graph path) as last_match
from symptom as s1,
    syndrome_causes for path as sc1,
    symptom         for path as s2
where match(
    shortest_path(s1(-(sc1)->s2)+)
    )
    and s1.symptom_name = 'Cardiovascular'

我曾考虑在WHERE 子句中添加另一个过滤条件,但我不知道如何编写它,以便我将节点限制为仅通过 “链接”中的值 列。

请注意,我知道,我可以将 links 列中的值传递给它以过滤行并让我获得 last_match 中的“Days”值列,但我的意图是通过节点的特定路径(验证)来过滤它,如下所示:

where match(
    shortest_path(s1(-(sc1)->s2)+)
    )
    and s1.symptom_name = 'Cardiovascular'
    and s2.symptom_name = ALL('Palpitations, 'Duration', 'Interval', 'Days')

但是,这给出了一个错误:

别名或标识符“s2.symptom_name”不能在选择中使用 列出、排序、分组或有上下文。

那么,有没有办法限制路径搜索通过某些节点?

【问题讨论】:

    标签: sql-server sql-server-2019 sql-graph sql-server-graph graph-table


    【解决方案1】:

    那么,有没有办法限制路径搜索通过某些 节点?

    可以通过仅选择“for path”的特定值来过滤/限制要考虑用于路径的节点,但这并不规定如何遍历路径。

    ......
    from symptom as s1,
    syndrome_causes for path as sc1,
    (select * from symptom where symptom_name in ('Palpitations', 'Duration', 'Interval', 'Days')) for path as s2
    ......
    

    【讨论】:

    • 用于过滤 last_match 和/或 path/[links],派生整个查询:select * from (select distinct...........and s1.symptom_name = 'Cardiovascular') as drv where drv.last_match = 'Days' and drv.links=concat('Palpilations', '->', 'Duration',.......'Days')
    猜你喜欢
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多