如何删除重复数据并通过相同数据字段计算数字

时间:2018-01-10 10:41:23

标签: python django database

我使用python和django。

我想通过字段'agent__id'和'帐户'删除重复数据,并计算字段' agent__id'通过相同的数据。我的代码如下:

account_id  agent_id
30001       1 
30001       2
30001       2

数据如下:

agent_id agent_count
1        1
2        2

代码打印如下:

agent_id agent_count
1        1
2        1

我希望数据如下:

System.IO.FileLoadException: Loading this assembly would produce a different grant set from other instances.

1 个答案:

答案 0 :(得分:0)

我解决了问题,我们应该使用属性' distinct'在对象'计数' ,main方法的源代码如下:

class Count(Aggregate):

def __init__(self, expression, distinct=False, **extra):
    if expression == '*':
        expression = Star()
    super(Count, self).__init__(
        expression, distinct='DISTINCT ' if distinct else '', output_field=IntegerField(), **extra)

因此,我们可以使用Count('field', distinct=True),正确的代码如下:

list(AgentPayLog.objects.filter(bill__status=DEAL_STATUS_PAID).distinct().values('agent__id').
                  annotate(agent_count=Count(F('account_id'), distinct=True)).
                  values('agent__id', 'agent_count'))