Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
07-25-2021 07:54 PM
Hi,
I have list of nodes with the same label (e.g :Car), there is a property call "type" (e.g SUV, Coupe, Sedan.....) which is used to differenciate the nodes, Now I would like to get the number of different types inside the list (e,g 25 SUVs, 35 Coupes, 40 Sedans). How could I do that?
Thanks,
Oli
07-25-2021 09:53 PM
match (a:Car)
with distinct a.type as type, count(a) as cnt
return type, cnt order by cnt desc
07-25-2021 10:13 PM
Thanks for your reply. I think the result is what I need. Just one thing is that all my nodes are in a list and I need to get the value based on the list, if you know how to do that?
oli
07-25-2021 10:25 PM
Please let me know what you mean by 'nodes are in a list'. I assume the node has property with list values. Post a sample of that property so that I can suggest an answer.
07-25-2021 10:51 PM
// suppost I randomly sample the car nodes
with collect(c) as car
// now I have a list called "cars"
// I wish to know the types and their corresponding amount within a list
with distinct a.type as type, count(a) as cnt
return type, cnt order by cnt desc
// The car structure is (c:car {carID, carType})
07-25-2021 11:01 PM
Try this:
// suppost I randomly sample the car nodes
with collect(c) as car
// now I have a list called "cars"
UNWIND car as car1
WITH car1.type as type, count(distinct car1) as cnt
return type, cnt order by cnt desc
// The car structure is (c:car {carID, carType})
07-25-2021 11:13 PM
07-25-2021 11:19 PM
Post your Cypher query that produced 100 samples and 2 or 3 types.
07-25-2021 11:29 PM
It works finally, mis-typed, my bad
By the way, I would like to ask that if it's possible for us to use uwind with case when to achieve this?
07-25-2021 11:46 PM
Send me the Cypher you used with CASE statement.
07-26-2021 12:08 AM
with collect(c) as car
unwind car as car1
case car1.type
when "SUC"
Then
// something like x = x + 1
when "Coupe"
Then
// y = y + 1
when "Sedan"
Then
// z = z + 1
end
// or maybe use foreach
foreach(ig in case when car1.type = "SUV") THEN [1] ELSE [] END | x = x + 1)
foreach(ig in case when car1.type = "Coupe") THEN [1] ELSE [] END | y = y + 1)
foreach(ig in case when car1.type = "Sedan") THEN [1] ELSE [] END | z = z + 1)
07-27-2021 12:49 AM
It would be better to count based on type, then sum over the counts:
...
with collect(c) as cars
UNWIND cars as car
WITH car.type as type, count(distinct car) as cnt
WITH CASE WHEN type = 'SUV' THEN cnt ELSE 0 END as suvs,
CASE WHEN type = 'Coupe' THEN cnt ELSE 0 END as coupes,
CASE WHEN type = 'Sedan' THEN cnt ELSE 0 END as sedans
RETURN sum(suvs) as suvs, sum(coupes) as coupes, sum(sedans) as sedans
All the sessions of the conference are now available online