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 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?
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.
// 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})
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})
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)
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