Django queryset聚合计数计数错误的事情 [英] django queryset aggregation count counting wrong thing

查看:122
本文介绍了Django queryset聚合计数计数错误的事情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是来自以下方面的延续问题:

Django查询集相对于其他列获得不同的列值

This is a continuation question from:
Django queryset get distinct column values with respect to other column

我的问题:

在Django中使用总计计数会在查询集中计数错误,或者据我所知甚至在查询集中也没有。

Using aggregate count in Django counts the wrong thing in the queryset, or as far as I can see something that is not even in my queryset.

我所做的我使用的

What I did I used:

queryset.order_by('col1', 'col2').distinct('col1', 'col2').values('col2')

获取模型的 col2 的值,其中所有行在中具有一对不同的值(col1,col2) 。上面的链接中有一个示例。我打印了查询集,看起来不错,我有

to get the values of col2 of a model where all the rows have a distinct pair of values in (col1, col2). There is an example in the link above. I printed my queryset and it looks good, I have

[{'col2': value1}, ... , {'col2': value1},{'col2': value2}, ..., {'col2': value2},...]

我现在要计算每个值在查询集中显示的数量。我使用聚合计数进行此操作。我有:

I now want to count how much each value appears in the queryset I got. I do this using aggregation count. I have:

a = {'count': Count(F(col2), distinct=False)}
queryset.annotate(**a)

我也尝试过使用``distinct = True`没有运气

I tried this with ``distinct=True` as well but no luck

我希望得到 [{col2:value1,count:num1},{col2:value2,count:num2} .. 。]

相反,我得到了 [{col2:value1,count:num11},{col2:value1,count:num12},... ,{col2:value1,计数:num_1n},{col2:value2,计数:num21},...,{col2:value1,计数:num_2n},...]
据我所知num11,....,num_1n是col2中存在的行1的数量,其中col1中有任何特定值,在我之前使用 order_by('col1' ,'col2')。distinct('col1','col2')。values('col2')在准备查询时。

I would expect to get [{col2:value1, count: num1}, {col2:value2, count: num2} ...].
Instead I get [{col2: value1, count: num11}, {col2: value1, count: num12}, ... ,{col2: value1, count: num_1n}, {col2: value2, count: num21}, ... ,{col2: value1, count: num_2n}, ...]. Where as far as I can tell num11, ...., num_1n are the amount of lines value1 existed in col2 with any specific value in col1, previous to me using order_by('col1', 'col2').distinct('col1', 'col2').values('col2') when preparing the query.

我不知道是什么原因造成的。我尝试查看 queryset.query 参数,但我不明白我要去哪里。

I can't figure out what can cause this. I tried looking in the queryset.query parameter but I can't understand where I am going wrong.

任何

推荐答案

.order_by 仅应指定'col2',例如:

queryset.values('col2').annotate(
    count=Count('col1', distinct=True)
).order_by('col2')

因此,这将产生一个 QuerySet ,其外观如下:

This will thus yield a QuerySet that looks like:

< QuerySet [
    {'col2': 1, 'count': 4 },
    {'col2': 2, 'count': 2 }
]>

所以这意味着<$ c $有四个 distinct 值给定 col2 的c> col1 的值为 1 ,而<$ c为两个不同的值给定 col2 的$ c> col1 的值为 2

So that means that there are four distinct values for col1 given col2 has value 1, and two distinct values for col1 given col2 has value 2.

这将构造一个查询,例如:

This will construct a query like:

SELECT col2, COUNT(DISTINCT col1) AS count
FROM some_table
GROUP BY col2
ORDER BY col2

code> .distinct(..)在这里不是必需的,因为由于 GROUP BY 仅获得不同的 col2 值,并且由于我们 COUNT(DISTINCT ..),因此这意味着每个 col1 的值被计算一次。

The .distinct(..) is here not necessary since due to the GROUP BY we will only obtain distinct col2 values, and because we COUNT(DISTINCT ..) this thus means that each distinct value of col1 is counted once.

这篇关于Django queryset聚合计数计数错误的事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆