我使用Django查询:
sports.PYST.objects.using( 'sports-data' ).all().values('season','player','team').annotate(max_count = Max('punt_long') ).query
它给o / p像:
SELECT `PYST`.`SEASON`, `PYST`.`PLAYER`, `PYST`.`TEAM`, MAX(`PYST`.`PUNT_LONG`) AS `max_count` FROM `PYST` GROUP BY `PYST`.`SEASON`, `PYST`.`PLAYER`, `PYST`.`TEAM` ORDER BY NULL
我的期望:
select season,player,team,max(punt_long)as punt_long from PYST group by season
任何人都可以提供帮助或需要任何其他信息吗?
答案 0 :(得分:0)
如果没有:
,我认为这是不可能的修改1:
关于解决方案2号。这仍然不是最好的想法,但它是我能想到的最快的:
from django.db.models import Max, Q
from operator import __or__ as OR
result_dict = Score.objects.values('season').annotate(Max('punt'))
q = [Q(season=row['season']) & Q(punt=row['punt__max']) for row in result_dict]
qs = Score.objects.filter(reduce(OR, q))
查看此链接了解更多详情: http://css.dzone.com/articles/best-way-or-list-django-orm-q
答案 1 :(得分:0)
现在为时已晚,但我希望今天能为某人工作:
(...)
生成以下查询:
Model.objects.filter(name__in=["foo", "foo1"]).values('last_name')\
.order_by('id')\
.annotate(total=Count('id')).values('first_name')