Reputation: 5540
I have a database named 'Employee' in my application:
class Employee(db.Model):
id = db.IntegerProperty(required = True)
name = db.StringProperty(required = True)
role = db.StringProperty()
date_of_joining = db.StringProperty()
salary = db.IntegerProperty()
current_task = db.TextProperty()
percentage_task = db.FloatProperty()
Now, how could I sum the salary of all the employees whose entries are stored in the 'Employee' database?
Upvotes: 0
Views: 67
Reputation: 4692
If that's what you currently have, you'd have to get the whole list of 'Employee' and then iterate through them to get a running total, as the datastore doesn't natively support aggregation functions.
Something like :
employees = db.Query(Employee, projection=('salary'))
for employee in employees:
salarySum += employee.salary
On the other hand, if the salary sum is something you need relatively often, it might be a better idea to keep a counter totaling the salaries, updating it at the same time you update salaries (if the updates are frequent, you might need to look into sharding counters to make sure you don't run into contention problems).
Upvotes: 2