Neo4j Cypher join group by having result - neo4j

I have the following query in SQL and I am trying to convert to cypher
select *, sum(quantity) as sum_qty from table1 inner join table2
on table1.t_id = table2.t_id_hd
group by table1.t_id
having sum_qty = table2.total_qty
The Cypher query I have so far is this
MATCH (a:table1)-[:parent]-(b:table2)
WITH a.t_id, sum(a.quantity) as sum_qty, b
WHERE sum_qty = b.total_qty
RETURN a.t_id, b.t_id_hd
This doesn't quite work because there seems to be duplicate b.t_id_hd

Related

Create union using TypeOrm Query Builder

I have a subquery that is a union of two queries that I am quering from. I need to achieve the same results using TypeOrm. My query is as below.
select port_id,v.port_name,g.name as country from (
select destination_port_id as port_id from vessels
union
select last_port_id as port_id from vessels) as ports
join vessels_ports v on port_id=v.id join global_countries g on g.id = v.country_id
order by port_id asc;

Returning subquery in neo4j based on two different where conditions

I have this query in SQL:
Select Id, CrawlerId,CrawlerName,
(SELECT Count(*) from CrawlerResult cr where cr.CrawlerId = cs.CrawlerId and IsNew=1) as LastRunResult ,
(SELECT Count(*) from CrawlerResult cr where cr.CrawlerId = cs.CrawlerId ) as TotalResult
FROM CrawlerScheduler cs
How to convert this query to neo4j cypher by combining CrawlerScheduler and CrawlerResult nodes?
I'm assuming you've replaced the foreign key relationships from SQL with actual relationships in Cypher, and that you're using actual booleans instead of 1 and 0? Something like:
(:CrawlerScheduler)-[:RESULT]->(:CrawlerResult)
If so then the equivalent Cypher query might look like this:
MATCH (cs:CrawlerScheduler)
WITH cs, SIZE((cs)-[:RESULT]->()) as TotalResult
OPTIONAL MATCH (cs)-[:RESULT]->(cr)
WHERE cr.IsNew
WITH cs, TotalResult, COUNT(cr) as LastRunResult
RETURN cs.Id, cs.CrawlerId, cs.CrawlerName, LastRunResult, TotalResult
EDIT
I changed the second match to an OPTIONAL MATCH, just in case the scheduler didn't have results, or didn't have new results.

Solr outer join / not join query

I may be asking too much but I want to do a left outer join between two cores
and get data from A only where B does not have related data.
Following is exactly my equivalent SQL query (for simplicity I have removed other conditions),
1. SELECT A.* FROM A AS A
WHERE A.ID NOT IN (SELECT B.A_ID FROM B AS B WHERE B.STATUS_ID != 1)
I understand that solr join is actually subquery, I need data from only A.
It would be very easy if the not was not there in where condition for sub query.
For example,
2. SELECT A.* FROM A AS A
WHERE A.ID IN (SELECT B.A_ID FROM B AS B WHERE B.STATUS_ID != 1)
I can have q={!join from=aId to=id fromIndex=b}(-statusId:1).
How can I do a nagete here, i.e. solr query for 1

Can Neo4j Cypher query do similar thing as "Having" in SQL?

SQL has "Having" clause, for example:
SELECT LastName, COUNT(*)
FROM Employees
GROUP BY LastName
HAVING COUNT(*) > 10;
In Cypher, we can do count()
START n=node(2)
MATCH (n)-[r]->()
RETURN type(r), count(*)
But does Cypher have similar function as "Having", or is there any workaround?
Sure, having is just one of the many uses of query chaining with WITH which is similar to RETURN but determines which elements will be available in the next query part. WITH also supports ordering and paging.
START n=node(2)
MATCH (n)-[r]->()
WITH type(r) as t, count(*) as c
WHERE c > 10
RETURN t,c

oracle outer join query

I have 3 oracle tables. A joins to B and B joins to C. I want all records from A irerspective of whether a corresponding record exists in B or C. I wrote a query like this:
select a.name from a,b,c where a.a_id = b.b_id(+) and b.b_id = c.c_id(+)
This query does not seem right to me, particularly with the second join. What will exactly happen if there is a record in A but correspondingly nothing in B and C? Will it still fetch the record?
For some reason the above query returns same count of records as select a.name from a
So I am guessing that the query is right? Also is there a better way to rewrite the query?
I presume the better query can be
Select a.name from A a left join B b on a.a_id=b.b_id inner join C c on b.b_id=c.c_id
This should give the result as you have expected
http://rajanmaharjan.com.np

Resources