In the Introduction to Graph Query Language post we were looking at the circular relationship between a Customer and Tender (mode of payment) as used in Transactions
To recap:
- Customer may use a Tender (mode of payment e.g. Credit Card, Gift Card etc);
- A Tender (mode of payment) can be used by Customer
Here the the relationship is bi-directional with different context. Note that Customer may use several different Tenders in different transactions e.g. The Customer may use a Gift Card for Transaction 1 and Credit Card for Transaction 2. Also Note that a Single Tender may be used by several Customer for e.g. a Husband and Wife using the same Gift Card to pay for two different Transactions.
We had used following to retrieve the list of “Shared” Tenders
SELECT distinct *
FROM GRAPH_TABLE( PAYMENT_TENDER_GRAPH
MATCH (customer1 is customer) - [e1 is customer_paid_using] ->
(shared_tender is tender) - [e2 is tender_used_by] ->
(customer2 is customer)
where customer1.customer_id != customer2.customer_id
COLUMNS ( customer1.first_name as customer_1_name
, shared_tender.card_number as shared_tender
, customer2.first_name as customer_2_name)
);
Note that in the above MATCH
clause the pattern was defined as (node) – [edge] → (node) → [edge] → (node). This is going in one direction with using both customer_paid_using
and tender_used_by
edges.
Graph theory says that the same pattern also be redefined as (node) – [edge] -> (node) <- [edge] - (node)
using just the customer_paid_using
edge. This is why it is Edge Pattern Definitions have a direction in GQL.
An edge pattern has a direction, as edges in graphs do. Thus, (a) <-[]- (b)
specifies a case where b has an edge pointing at a, whereas (a) -[]-> (b)
looks for an edge in the opposite direction.
SELECT distinct *
FROM GRAPH_TABLE( PAYMENT_TENDER_GRAPH
MATCH (customer1 is customer) - [e1 is customer_paid_using] ->
(shared_tender is tender) <- [e2 is customer_paid_using]
- (customer2 is customer)
where customer1.customer_id != customer2.customer_id
COLUMNS (customer1.first_name as customer_1_name
, shared_tender.card_number as shared_tender
, customer2.first_name as customer_2_name)
);
This will produce the same result: