Adjacency matrix

Hi everyone

I would like to find the adjacency matrix of the following bipartite graph

redha_benhisse1_0-1669936093203.jpeg

if the link exists, the matrix takes the value revenue. otherwise, it takes 0.

MATCH (c:customer)
WITH collect(c) AS cust
UNWIND cust AS a
MATCH (p:part)
WITH collect(p) AS par
UNWIND par AS b
RETURN a.id AS cutomer, b.id AS part, 
CASE 
WHEN EXISTS((a)-[r:customer_to_part]->(b)) THEN r.revenue 
ELSE 0 
END AS value

But this does not work.

Can you please help me to find the right matrix

Just a couple notes. The order of the query operations will cause the 'match(p:part) collect(p)' block to be repeated for each customer node. Also, its not allowed to introduce a variable in a pattern expression, so returning r.revenue isn't allowed. I refactored your query to resolve these two issues.

match(c:customer)
with collect(c) as customers
match(p:part)
with customers, collect(p) as parts
unwind customers as customer
unwind parts as part
optional match (customer)-[r:customer_to_part]->(part)
return customer.id as cust_id, part.id as part_id,
case
    when r is not null then r.revenue
    else 0
end as value

Thanks glilienfield
(migrated from khoros post https://community.neo4j.com/t5/neo4j-graph-platform/adjacency-matrix/m-p/62799#M37100)