Problem 1 - Statement
Given the graph below, construct a query to return the encircled sub-graph:
- return Application with property sealId=?
- return all ApplicationModules owned by Application
- return all APIs provided for each ApplicationModule
- return Request with max version property for each API
|
|
|
Problem 1 - Solutions
CQL v4.0 With APOC (without relationships)
MATCH (app:Application {sealId: 1}) OPTIONAL MATCH (app)<-[r0:IS_OWNED_BY]-(appMod:ApplicationModule) OPTIONAL MATCH (appMod)-[r1:PROVIDES]->(api:Api) WITH collect(DISTINCT app) AS apps, collect(DISTINCT appMod) AS appMods, collect(DISTINCT api) AS apis UNWIND apis AS api OPTIONAL MATCH (api)<-[r2:IS_OWNED_BY]-(request:Request) WITH apps, appMods, apis, api, apoc.agg.maxItems(request, request.version).items AS requests WITH apps, appMods, apis, requests UNWIND requests AS request RETURN apps, appMods, apis, collect(request) AS requests
CQL v4.0 With APOC (with relationships)
If you want to return the relationships between APIs and Request nodes, then use the following query below
MATCH (app:Application {sealId: 1}) OPTIONAL MATCH (app)<-[r0:IS_OWNED_BY]-(appMod:ApplicationModule) OPTIONAL MATCH (appMod)-[r1:PROVIDES]->(api:Api) WITH collect(DISTINCT app) AS apps, collect(DISTINCT appMod) AS appMods, collect(DISTINCT api) AS apis UNWIND apis AS api OPTIONAL MATCH (api)<-[r2:IS_OWNED_BY]-(request:Request) WITH apps, appMods, apis, api, apoc.agg.maxItems(request, request.version).items AS requests, apoc.agg.maxItems(r2, request.version).items AS r2s WITH apps, appMods, apis, requests, r2s UNWIND requests AS request UNWIND r2s AS r2 RETURN apps, appMods, apis, collect(request) AS requests, collect(r2) AS r2s
Problem 2 - Statement
Given the graph below, construct a query to return the encircled sub-graph:
- return Application with property sealId=?
- return all ApplicationModules owned by Application
- return all APIs provided for each ApplicationModule (return only the relationship with highest version property) ← ADDITIONAL CONSTRAINT
- return Request with max version property for each API
/cql---data-manipulation-language-(dml)/cql---getting-max-by-property-foreach-group/getting-max-by-property-foreach-group---problem-2.png)
Problem 2 - Solution
CQL v4.0 With APOC
MATCH (app:Application {sealId: 1}) OPTIONAL MATCH (app)<-[r0:IS_OWNED_BY]-(appMod:ApplicationModule) WITH collect(DISTINCT app) AS apps, collect(DISTINCT appMod) AS appMods, collect(DISTINCT r0) AS r0s OPTIONAL MATCH (appMod)-[r1:PROVIDES]->(api:Api) WITH apps, appMods, r0s, apoc.agg.maxItems(r1, r1.version).items AS grouped_r1s, api UNWIND grouped_r1s AS r1 WITH apps, appMods, r0s, collect(DISTINCT r1) AS r1s, collect(DISTINCT api) AS apis UNWIND apis AS api OPTIONAL MATCH (api)<-[r2:IS_OWNED_BY]-(request:Request) WITH apps, r0s, appMods, r1s, apis, api, apoc.agg.maxItems(r2, request.version).items AS r2s, apoc.agg.maxItems(request, request.version).items AS requests WITH apps, r0s, appMods, r1s, apis, r2s, requests UNWIND r2s AS r2 UNWIND requests AS request RETURN apps, r0s, appMods, r1s, apis, collect(r2) AS r2s, collect(request) AS requests
/cql---data-manipulation-language-(dml)/cql---getting-max-by-property-foreach-group/getting-max-by-property-foreach-group---problem-statement.png)
/cql---data-manipulation-language-(dml)/cql---getting-max-by-property-foreach-group/getting-max-by-property-foreach-group---solution-1.png)
/cql---data-manipulation-language-(dml)/cql---getting-max-by-property-foreach-group/getting-max-by-property-foreach-group---solution-2.png)