简体   繁体   English

sql server图查询,找到所有节点的路径

[英]sql server graph query, find all paths to node

I have a very common problem I am trying to solve using graph queries (sql server 2017).我有一个非常常见的问题,我试图使用图形查询(sql server 2017)来解决。

在此处输入图片说明

  1. I want to build a query and find how anyone in the nodes is connected to C .我想构建一个查询并找出节点中的任何人如何连接到C
  2. I want to build a query and find how anyone in the nodes is connected to C (with 1 or 2 connections).我想构建一个查询并找出节点中的任何人如何连接到C (具有 1 或 2 个连接)。

here is the full script to create this graph:这是创建此图的完整脚本:

DROP TABLE IF EXISTS Person;
CREATE TABLE Person (userName VARCHAR(100)  PRIMARY KEY) AS NODE;

INSERT INTO Person (userName) VALUES ('A'),('B'),('C'),('D'),('E'),('F'); 

DROP TABLE IF EXISTS Follow; 
CREATE TABLE Follow AS EDGE;

INSERT INTO Follow ($from_id, $to_id) VALUES (
   (SELECT $node_id FROM dbo.Person WHERE userName = 'A'),
   (SELECT $node_id FROM dbo.Person WHERE userName = 'E')),

   ((SELECT $node_id FROM dbo.Person WHERE userName = 'E'),
   (SELECT $node_id FROM dbo.Person WHERE userName = 'C')),


   ((SELECT $node_id FROM dbo.Person WHERE userName = 'C'),
   (SELECT $node_id FROM dbo.Person WHERE userName = 'A')),


   ((SELECT $node_id FROM dbo.Person WHERE userName = 'A'),
   (SELECT $node_id FROM dbo.Person WHERE userName = 'F')),

   ((SELECT $node_id FROM dbo.Person WHERE userName = 'F'),
   (SELECT $node_id FROM dbo.Person WHERE userName = 'B')),

   ((SELECT $node_id FROM dbo.Person WHERE userName = 'B'),
   (SELECT $node_id FROM dbo.Person WHERE userName = 'F')),

   ((SELECT $node_id FROM dbo.Person WHERE userName = 'B'),
   (SELECT $node_id FROM dbo.Person WHERE userName = 'E')),

   ((SELECT $node_id FROM dbo.Person WHERE userName = 'E'),
   (SELECT $node_id FROM dbo.Person WHERE userName = 'B'));

this query is not working since it is giving me only the direct relation:这个查询不起作用,因为它只给我直接关系:

SELECT Person1.userName as userName1, Person2.userName as userName2   
FROM Person as Person1, Follow, Person as Person2 
WHERE MATCH(Person1-(Follow)->Person2)
AND Person2.userName = 'C'

you can try something like below:您可以尝试以下操作:

SELECT 
        p1.userName, 
        p1.userName as StartNode,
        LAST_VALUE(p2.userName) WITHIN GROUP (GRAPH PATH) AS FinalNode,
        STRING_AGG(p2.userName,'->') WITHIN GROUP (GRAPH PATH) AS [Edges Path],
        COUNT(p2.userName) WITHIN GROUP (GRAPH PATH) AS Levels
    FROM
        dbo.Person p1,
        dbo.Person FOR PATH p2,
        dbo.Follow FOR PATH Follow
    WHERE 
        MATCH(SHORTEST_PATH(p1(-(Follow)->p2)+))
        AND p1.userName = 'C';

to find all the incoming connection for a node, we need to wrap the query and filter for the final node like below:要查找节点的所有传入连接,我们需要为最终节点包装查询和过滤器,如下所示:

SELECT
    username, StartNode, [Edges Path], FinalNode, Levels
FROM (
    SELECT 
        P1.username, 
        P1.username as StartNode, 
        STRING_AGG(P2.userName,'->') WITHIN GROUP (GRAPH PATH) AS [Edges Path],
        LAST_VALUE(P2.userName) WITHIN GROUP (GRAPH PATH) AS FinalNode,
        COUNT(P2.userName) WITHIN GROUP (GRAPH PATH) AS Levels
    FROM
        Person P1,
        Person FOR PATH P2,
        Follow FOR PATH Follow
    WHERE 
        MATCH(SHORTEST_PATH(P1(-(Follow)->P2)))
 ) AS Q
 WHERE Q.FinalNode = 'C'

to limit the the levels or number of hops, we can provide the recursion quantifiers in place of (+ --- one or more) like below:为了限制跳跃的级别或数量,我们可以提供递归量词来代替 (+ --- 一个或多个),如下所示:

SELECT
    username, StartNode, [Edges Path], FinalNode, Levels
FROM (
    SELECT 
        P1.username, 
        P1.username as StartNode, 
        STRING_AGG(P2.userName,'->') WITHIN GROUP (GRAPH PATH) AS [Edges Path],
        LAST_VALUE(P2.userName) WITHIN GROUP (GRAPH PATH) AS FinalNode,
        COUNT(P2.userName) WITHIN GROUP (GRAPH PATH) AS Levels
    FROM
        Person P1,
        Person FOR PATH P2,
        Follow FOR PATH Follow
    WHERE 
        MATCH(SHORTEST_PATH(P1(-(Follow)->P2){1,3}))
 ) AS Q
 WHERE Q.FinalNode = 'C'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过SQL Server的子节点中的值查找所有父节点 - How to find all parent node by a value in child node with SQL Server 使用 SQL Server Graph 2017 获取两个节点之间的所有路径 - Fetching all the paths between two nodes using SQL Server Graph 2017 SQL 服务器查询以查找数据库中所有用户的所有权限/访问权限 - SQL Server query to find all permissions/access for all users in a database 如何使用传递属性从 SQL Server 中的关系表中递归查找所有路径? - How to find all paths recursively from relational table in SQL Server using transitive property? 查询以查找所有FK约束及其删除规则(SQL Server) - Query to find all FK constraints and their delete rules (SQL Server) SQL Server查询以按订单查找所有产品的总价 - SQL Server query to find the total price of all the products by order 使用Query查找在SQL Server中使用游标的所有存储过程 - Find all the stored Procedures that use a cursor in SQL Server using Query SQL Server 查询以查找所有当前数据库名称 - SQL Server query to find all current database names 获取 SQL Server 图数据库中的所有朋友 - Get all friends in SQL Server graph database SQL 服务器图来获取连接到一个节点的多个节点类型 - SQL Server graph to fetch multiple node types connected to a node
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM