user2069959
user2069959

Reputation:

How to not return the same node in different columns in Cypher

I am trying to not return the same node twice in this query in separate columns

START n=node:nodes(customer = 'c2')
MATCH n-[:OWNS]->owned-[:CROSS_SELL|UP_SELL]->sell
RETURN distinct n.customer,owned.product,sell.product

Customer C2 owns products P1 and P2. Product P2 and P3 can be cross sold from product P1. Product P3 can be also be up sold from product P2.

The query correctly returns

C2 P1 P3
C2 P2 P3
C2 P1 P2

Since the customer already owns P2, I do not want the last record in the result.

How can I accomplish this?

Thanks

Upvotes: 1

Views: 398

Answers (2)

ulkas
ulkas

Reputation: 5918

simply add a condition where you filter up on relationship between C2 and P2

START n=node:nodes(customer = 'c2')
MATCH n-[:OWNS]->owned-[:CROSS_SELL|UP_SELL]->sell
WHERE not(n-[:OWNS]-sell)
RETURN distinct n.customer,owned.product,sell.product

Upvotes: 4

ean5533
ean5533

Reputation: 8994

I haven't tested to verify that this works, but it seems logically correct:

START n=node:nodes(customer = 'c2')
MATCH n-[:OWNS]->owned-[:CROSS_SELL|UP_SELL]->sell,
      alreadyOwns = n-[:OWNS]->sell
WHERE COUNT(alreadyOwns) = 0
RETURN distinct n.customer,owned.product,sell.product

The idea is basically to try and find a direct match from the start node to the cross/up-sold item and if a path is found then don't include it.

Upvotes: 0

Related Questions