繁体   English   中英

按两列分组,一列分开,按计数排序

[英]Group by two columns, distinct by one, and order by the count

与题名苦苦挣扎:)

我是python&django的初学者,我想查询一个查询

我的(简化)模型是:用户,旅行,国家。

用户可以在任意国家/地区创建自己想要的许多旅行。 他也可以创建多次前往同一国家的旅行。

我的目标是获取由不同用户创造的旅行次数最多的前15个国家/地区。 意味着如果一个用户创建了10次前往同一国家的旅行,则视为一次旅行。

到目前为止,我取得的成就是

    hottest_countries = models.Event.objects.values('country')\
                      .exclude(creator=None) \
                      .annotate(count=Count('country'))\
                      .distinct() \
                      .order_by('-count')[:15]

这将返回国家和每个国家的数量, 但不会返回不同的用户

所以我将代码更改为此

    hottest_countries = models.Event.objects.values_list('country', flat=True)
                      .exclude(creator=None) \
                      .annotate(count=Count('country'))\
                      .distinct() \
                      .order_by('-count')[:15]

    # Getting all the creators of each country
    creators_for_country = [models.Event.objects.values_list('creator', flat=True).filter(Q(country=country_id)).distinct() for country_id in hottest_countries]

    # Sorting again to make sure
    hots_events_sorted = [{"country_id": country_id, "count": len(creators_for_country[idx]), "creators": creators_for_country[idx]} for idx, country_id in enumerate(hottest_countries)]
    hots_events_sorted.sort(key=itemgetter('count'), reverse=True)

它正在工作,但是:

答:我认为这很复杂。 并且必须是更简单的方法。

B.可能是我在第一个查询中获取的前15个国家并不是真正正确的国家,因为可能是第二个查询在创建者区分时减少了条目分配。 对于前。 一位用户创造了1000次加拿大旅行。 这会将第一个查询中的国家/地区推到列表的顶部。 但是当我们按创建者区分列表时,我们会得到一个条目。 这使得加拿大名列前茅,甚至根本没有。

注意:当我尝试与给定的列进行区分时,出现数据库错误,即我的数据库不支持按列区分。

万一有人像我一样挣扎,这就是我的解决方案。

在注释中添加distinct=True解决了我的问题

hottest_countries = models.Event.objects.values('country')\
                  .exclude(creator=None) \
                  .annotate(count=Count('creator', distinct=True))\
                  .distinct() \
                  .order_by('-count')[:15]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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