Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
08-24-2022 09:07 AM - edited 08-24-2022 09:16 AM
I need some variables in Cypher script, see the use case:
City has three properties X,Y and Z
The value of Z should be calculated as Z = X + Y
Data and steps.
1) The data, already in the database (here only one node)
CREATE (a:City {DS: 'AAA'})
2) Adding the trhee properties ( I could not find how to set the datatype, this one has worked)
MATCH (a:City) WHERE a.DS= 'AAA'
SET a.X = toFloat(0.0)
SET a.Y = toFloat(0.0)
SET a.Z = toFloat(0.0)
3) There is a load csv, which sets the properties X and Y (it works fine)
MATCH (a:City) - setting the values X,Y
WHERE a.DS= 'AAA'
SET a.X = toFloat(2.5)
SET a.Y = toFloat(5.2)
4) The Z value is being calculated (it work as well)
MATCH (a:City) - setting the values X,Y
WHERE a.DS= 'AAA'
SET a.Z = toFloat(a.X) + toFloat(a.Y)
BTW: SET a.Z = a.X + a.Y doesn't work as expected, it concatenates the values 😞
In the last step I need variables for toFloat(a.X) and toFloat(a.Y) , because I have a complicated expression with using several time both of them.
How to do it ?
Thanks,
Michel
Solved! Go to Solution.
08-24-2022 12:22 PM
Try this: Use SUM
SUM(toFloat(a.X) + toFloat(a.Y)) = 7.7
SET a.Z = SUM(toFloat(a.X) + toFloat(a.Y))
08-24-2022 01:02 PM
In the last step I need variables for toFloat(a.X) and toFloat(a.Y) , because I have a complicated expression with using several time both of them.
WITH toFloat(a.X) as x1, toFloat(a.Y) as x2
SET a.Z = SUM(x1 + x2)
08-24-2022 01:52 PM
Something is not correct. You should be able to add float numbers without wrapping in toFloat(). The only thing I can think of is that you imported the numbers in without using toFloat(). This is required, as all values are imported as strings, even things that represent numbers. This aligns with the behavior you observed that 'a.X + a.Y' concatenated the two values. This will happen if the two operands are strings.
That being said, you just need to use 'with' statements to compute new values and pass to be used. Here is an example using your query.
MATCH (a:City)
WHERE a.DS = 'AAA'
WITH a, toFloat(a.X) as x, toFloat(a.Y) as y
WITH a, x + y as z
SET a.z = z
08-24-2022 12:22 PM
Try this: Use SUM
SUM(toFloat(a.X) + toFloat(a.Y)) = 7.7
SET a.Z = SUM(toFloat(a.X) + toFloat(a.Y))
08-24-2022 01:02 PM
In the last step I need variables for toFloat(a.X) and toFloat(a.Y) , because I have a complicated expression with using several time both of them.
WITH toFloat(a.X) as x1, toFloat(a.Y) as x2
SET a.Z = SUM(x1 + x2)
08-24-2022 01:52 PM
Something is not correct. You should be able to add float numbers without wrapping in toFloat(). The only thing I can think of is that you imported the numbers in without using toFloat(). This is required, as all values are imported as strings, even things that represent numbers. This aligns with the behavior you observed that 'a.X + a.Y' concatenated the two values. This will happen if the two operands are strings.
That being said, you just need to use 'with' statements to compute new values and pass to be used. Here is an example using your query.
MATCH (a:City)
WHERE a.DS = 'AAA'
WITH a, toFloat(a.X) as x, toFloat(a.Y) as y
WITH a, x + y as z
SET a.z = z
All the sessions of the conference are now available online