How to find the max value per group in NDB

2019-08-18 03:31发布

I'm used python with ndb in my app. How to find the max value per group in NDB?

My table

user_id | level_number | score
--------+--------------+--------------------
      1 | 1            | 15
      1 | 1            | 5
      1 | 2            | 26
      1 | 2            | 30

I want to get max score each level

1条回答
Summer. ? 凉城
2楼-- · 2019-08-18 03:52

From what I can see, the best would be to do a query with a sort order on the score where level_number = 1, then 2, then 3. Since the datastore cannot do computations, you'll need to use the sorting and just keep the highest one, if you don't want to do computations in your code.

I would personally have the scores linked to a parent entity "level", and then query per ancestor and check the highest score.

查看更多
登录 后发表回答