简体   繁体   中英

How can I store the cypher query value into a variable inside python function?

I want to store the price average of the data that I have inside my neo4j database, so I can then use the python variable to define if the product is cheap or expensive.

I already have the cypher query which is giving me all the average prices of all services that I have in my database. Now I want to store this query result into a variable or list. So I can use it to define if the price is cheap or expensive (if price <= average) it is cheap otherwise it is expensive.


      def average(self):

           query = '''
              MATCH (p:Price)-[:COSTS]-(s:Service)
              RETURN avg(toFloat(p.monthly)) AS Average, p.currency AS Currency, s.service_name AS Service
               '''
           return  graph.run(query, average= self.Average, currency= self.Currency, service_name=self.Service )

Can anyone help me out with this problem?

Your query can return multiple rows since it specifies Currency and Service as the unique grouping keys for the AVG aggregating function . So average() should probably return something like a list of dictionaries.

The run() function returns a Cursor , which has a data() function that will "extract the entire result as a list of dictionaries".

So, this version of average() should return a list of dictionaries for the query results:

  def average(self):
       query = '''
          MATCH (p:Price)-[:COSTS]-(s:Service)
          RETURN avg(toFloat(p.monthly)) AS Average, p.currency AS Currency, s.service_name AS Service
           '''
       return graph.run(query).data()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM