Django Group by query?



我正在使用django orm使用查询组来获取数据。RAW POSTGRESQL查询是 -

select date_trunc('day', time) as Time ,count(status) from table where group_id='2177' and status=1 group by date_trunc('day', time) order by time desc limit 7 offset 0;

它正在返回正确的输出。

        time              | count 
---------------------------+-------
2017-05-04 00:00:00+05:30 |    12
2017-05-03 00:00:00+05:30 |    26
2017-05-02 00:00:00+05:30 |    25
2017-05-01 00:00:00+05:30 |    26
2017-04-30 00:00:00+05:30 |    26
2017-04-29 00:00:00+05:30 |    26
2017-04-28 00:00:00+05:30 |    26

(7行(

我正在使用django注释函数来实现这一点 - 这是django查询 -

  records = TableModel.objects.filter(                  
                       group_id=group_id,                                           
                       status=1,                                                                       
                       time__range = get_time_range(range)                                                                    
                       ).annotate(
                                  period=DateTrunc('day', 'time') ,
                                  count=Count('status')
                               ).annotate(
                                         period = DateTrunc('day', 'time')
                               ).order_by('-time')

在调试中,我发现Django内部将其转换为 -

   SELECT "table"."id", "table"."time", "table"."status", "table"."group_id", DATE_TRUNC('day', "table"."time") AS "period", COUNT("table"."status") AS "count" FROM "table" WHERE ("table"."group_id" = '2177' AND "table"."status" = 1 AND "table"."time" BETWEEN '2017-04-28 11:47:21.421755+00:00' AND '2017-05-05 11:47:21.421755+00:00') GROUP BY "table"."id", DATE_TRUNC('day', "table"."time") ORDER BY "table"."time" DESC;

这是其O/P(其中包含更多的行( -

     id   |           time            | status               | group_id      |          period           | count 
  --------+---------------------------+----------------------+------------+---------------------------+-------
   267821 | 2017-05-04 10:36:13+05:30 |                    1 | 2177       | 2017-05-04 00:00:00+05:30 |     1
   267790 | 2017-05-04 09:36:35+05:30 |                    1 | 2177       | 2017-05-04 00:00:00+05:30 |     1
   267786 | 2017-05-04 09:30:44+05:30 |                    1 | 2177       | 2017-05-04 00:00:00+05:30 |     1
   267735 | 2017-05-04 08:36:09+05:30 |                    1 | 2177       | 2017-05-04 00:00:00+05:30 |     1
   267696 | 2017-05-04 07:36:32+05:30 |                    1 | 2177       | 2017-05-04 00:00:00+05:30 |     1
   267650 | 2017-05-04 06:36:14+05:30 |                    1 | 2177       | 2017-05-04 00:00:00+05:30 |     1
   267603 | 2017-05-04 05:36:14+05:30 |                    1 | 2177       | 2017-05-04 00:00:00+05:30 |     1
    ....
    ....
    (149 rows)

即默认情况下,它通过使用( id date_trunc('day','table',time''time''。y django orm查询。结果,输出将被更改。

默认情况下,django orm是否考虑了ID字段?有什么方法可以解决吗?

尝试此

TableModel.objects.filter(                  
       group_id=group_id,                                           
       status=1,                                                                       
       time__range = get_time_range(range)                                                                    
   ).annotate(
          period=DateTrunc('day', 'time') # Truncate to day and add to select query
   ).values('period') # Group By period
   .annotate(count=Count('status'))  # add count of the grouping to select
   .values('period', 'count') 
   .order_by('-time')

可以通过 -

来修复
  SELECT 1 as id ,DATE_TRUNC('day', "table"."time") AS "period", COUNT("table"."status") AS "count" FROM "table" WHERE "table"."group_id" = '2177' and "table"."status"=1 and time >= '2017-04-28 12:48:33.348682+00:00' and time <= '2017-05-05 12:48:33.348682+00:00' GROUP BY DATE_TRUNC('day', "table"."time"),period ORDER BY period DESC limit {} offset {};

默认情况下,django需要返回ID(primary_key(字段。

最新更新