开发者

Group by and distinct values in group

I a have a log object that have users, and dates etc in it.

I would like to group by user, but only the distinct dates per user must be counted in the group.

Something like:

Log.objects.values('user').annotate(count=Count('date')).distinct("date)

So in the end I would have a count of each user with log entries on a开发者_C百科 destinct dates.


Based on your comment, I think this is what you want:

Log.objects.values('user').annotate(number_of_days=Count('date', distinct=True))

This will give you a list of users and corresponding number of days that they had one or more log entries. This won't give you which dates, just a count of how many days.

Leaving the original answer as it is below:


I am not exactly clear on the question, but I think one of the two scenarios below covers what you want:

If you want a count of distinct users with log entries on every distinct date, this is what you need:

Log.objects.values('date').annotate(number_of_users=Count('user', distinct=True))

This will give you a list with dates and the corresponding number of users who had one or more Log entries on that date.

If you want a count of the number of Log entries each user made on each distinct date, this could work, but I haven't tried it:

Log.objects.values('date','user').annotate(number_of_log_entries=Count('id'))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜