django 中group by 以及sum count

原生SQL

SELECT order_id,city,locality,login_time,sum(morning_hours),sum(afternoon_hours),sum(evening_hours),sum(total_hours) 
FROM orders 
GROUPBY order_id,city,locality,login_time`

group by … sum

from django.db.models import Sum

Your_Model.objects.values(
    "order_id", "city", "locality", "login_time"
).order_by().annotate(
    Sum("morning_hours"),
    Sum("afternoon_hours"),
    Sum("evening_hours"),
    Sum("total_hours"),
)

group by …count

 from django.db.models import Count

 result = Books.objects.values('author')
                       .order_by('author')
                       .annotate(count=Count('author'))

https://docs.djangoproject.com/en/4.2/topics/db/aggregation/

相关推荐

  1. django group by 以及sum count

    2024-01-20 08:16:02       58 阅读
  2. django ormvalue和value_list以及转成list

    2024-01-20 08:16:02       27 阅读
  3. 初识Django,以及路由设置

    2024-01-20 08:16:02       48 阅读
  4. Django——Auth模块以及admin站点

    2024-01-20 08:16:02       33 阅读
  5. Django orm高级用法以及查询优化

    2024-01-20 08:16:02       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-20 08:16:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-20 08:16:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-20 08:16:02       87 阅读
  4. Python语言-面向对象

    2024-01-20 08:16:02       96 阅读

热门阅读

  1. Webpack5入门到原理13:开发服务器&自动化

    2024-01-20 08:16:02       60 阅读
  2. Linux 常用命令分享与示例

    2024-01-20 08:16:02       34 阅读
  3. 解决更新Xcode 15.2后,下载 iOS_17 Simulator失败

    2024-01-20 08:16:02       56 阅读
  4. Apache Flink 1.15正式发布

    2024-01-20 08:16:02       49 阅读
  5. 【LeetCode2744】最大字符串配对数目

    2024-01-20 08:16:02       61 阅读
  6. 获取mac地址,内网ip,当前ip位置信息

    2024-01-20 08:16:02       45 阅读
  7. Redis Interview Speedrun

    2024-01-20 08:16:02       51 阅读