How to find the max value per group in NDB

China☆狼群 提交于 2019-12-12 01:59:30

问题


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:


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.



来源:https://stackoverflow.com/questions/25241274/how-to-find-the-max-value-per-group-in-ndb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!