假设您有一个具有以下设置的图表:
CREATE (a:Part {'part_num': '123'}), (b:Part {'part_num': '345'}),
(c:Part {'part_num': '456'}), (d:Part {'part_num': '789'});
MATCH (a:Part {'part_num': '123'}), (b:Part {'part_num': '345'})
CREATE (a)-[u:used_by { quantity: 1 }]->(b);
MATCH (b:Part {'part_num': '345'}), (c:Part {'part_num': '456'})
CREATE (b)-[u:used_by { quantity: 2 }]->(c);
MATCH (a:Part {'part_num': '123'}), (d:Part {'part_num': '789'})
CREATE (a)-[u:used_by { quantity: 1 }]->(d);
然后你会得到下图:
使用您的基本查询,我们得到一个 JSON 数组
MATCH (a:part {part_num: '123'})-[u:used_by*]->(b:part {part_num: '456'})
RETURN [x IN u::jsonb | x.properties.quantity] AS quantities
-- Results
quantities
------------
[1, 2]
(1 row)
最后,我们只需要利用 AgnsGraph 的混合 SQL/Cypher 功能来转换该数组jsonb_array_elements_text
,然后使用SUM
该子查询。
SELECT sum(quantity::int) AS sum_of_used_by
FROM (
SELECT jsonb_array_elements_text(paths.quantity) AS quantity
FROM (
MATCH (a:part {part_num: '123'})-[u:used_by*]->(b:part {part_num: '456'})
RETURN [x IN u::jsonb | x.properties.quantity] AS quantity
) AS paths
) as total
-- Results
sum_of_used_by
----------------
3
(1 row)