Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Combine Multiple WITH and MATCH clause in Cypher Neo4j

I am working on a question consist of two different parts related to each other. I want to combine both of them.

1: First Part

MATCH (r:Route)-[source_Airport_ID]->(a:Airport)
with count(r.SourceAirportID) as cnt, a.AirportID as          
NumberofAirports_Having_source_flightGreater300
where cnt>300
Return Count(NumberofAirports_Having_source_flightGreater300) 

2: Second Part

MATCH (r:Route)-[destination_Airport_ID]->(a:Airport) 
with count(r.SourceAirportID) as cnt, a.AirportID as  
NmberofAirports_Having_destination_flightGreater300
where cnt>300
Return Count(NumberofAirports_Having_destination_flightGreater300)

3: Combine Part

MATCH (r:Route)-[source_Airport_ID]->(a:Airport) 
with count(r.SourceAirportID) as cnt, a.AirportID as   
NumberofAirports_Having_source_flightGreater300
where cnt>300

MATCH (r:Route)-[destination_Airport_ID]->(a:Airport) 
with count(r.SourceAirportID) as cnt, a.AirportID as     
NumberofAirports_Having_destination_flightGreater300,      
NumberofAirports_Having_source_flightGreater300
where cnt>300

Return      
Count(NumberofAirports_Having_source_flightGreater300),
Count(NumberofAirports_Having_destination_flightGreater300)

1: First one gives count 104

2: Second query also gives 104

3: But when I combine both of them answer is 10816. How to combine both and the answer still remain 104 for both?

like image 625
Ahsan Ali Avatar asked Oct 20 '25 05:10

Ahsan Ali


1 Answers

This should work:

MATCH (r:Route)-[source_Airport_ID]->(a:Airport)
with count(r.SourceAirportID) as cnt, a.AirportID as          
NumberofAirports_Having_source_flightGreater300
where cnt>300
WITH COUNT(NumberofAirports_Having_source_flightGreater300) AS cnt1

MATCH (r:Route)-[destination_Airport_ID]->(a:Airport) 
WITH cnt1, count(r.SourceAirportID) as cnt, a.AirportID as  
NmberofAirports_Having_destination_flightGreater300
where cnt>300
Return cnt1, Count(NumberofAirports_Having_destination_flightGreater300) AS cnt2
like image 89
cybersam Avatar answered Oct 22 '25 18:10

cybersam