当前位置:网站首页>PostgreSQL server programming aggregation and grouping
PostgreSQL server programming aggregation and grouping
2022-07-04 22:20:00 【Hua Weiyun】
In any type of ORM in , polymerization (aggregation) Are the root causes of chaos , and Django So it is with . This document provides various examples , Demonstrates how to use Django Of ORM Group data (group) And aggregation (aggregation), But I decided to study from another angle .
In this paper , I will QuerySet and SQL Place... Side by side . If you like it best SQL Language , Then this is for you Django GROUP BY Quick reference table .
How to be in Django Middle group
To demonstrate different GROUP BY Inquire about , I will use Django built-in django.contrib.auth
Models in applications .
How to calculate the number of rows
Let's calculate how many users we have :
Counting rows is very common , So much so that Django stay QuerySet It contains a function for it . And others QuerySet Different , We'll see it next count
Return a number .
How to use aggregate functions
Django There are also two other ways to count the number of rows in the table .
We will start from aggregate
Start :
In order to use aggregate
We imported the aggregate function Count
. under these circumstances , We use the name of the primary key column id
To count all the rows in the table .
The result of aggregation is a dictionary , As shown below :
Return the default key name of the dictionary key yes id__count. It is best not to rely on this naming convention , Instead, provide your own name :
How to group
Use aggregate
We get the aggregate function ( such as Count, Max, Sum) Results applied to the entire table , It's very useful , But usually we want to divide the records in the table into groups (group), Then apply the aggregate function to each group .
Now let's group according to the activity status of users , Then count the number of people in each group :
This time we used annotate
. In order to produce GROUP BY The combination we use values
and annotate
:
values('is_active')
: According to what groupannotate(total=Count('id'))
: Aggregate what
The return is is_active And the number of people in each state .
Order matters : Calling values
Method used before annotate
Aggregate function results are not added as comments .
How to filter query sets before grouping and aggregating
To apply the aggregate function to the filtered query set , It can be used anywhere in the query filter
. for example , Only company employees are counted (staff) Active state count of :
How to sort query sets before using grouping aggregation
Like a filter , Can be used anywhere in the query statement order_by Sort the query set :
Be careful : You can check the fields in the table at the same time (is_active) And the fields generated after aggregation (total) Sort .
How to merge the results of multiple aggregate functions
Use multiple aggregate functions for the same set of data , Please add more than one comment at a time :
This query will generate the number of active and inactive users , And the last date that users in each group joined .
How to group by multiple fields
It's like executing multiple aggregate functions , We may also want to group by multiple fields in the table . for example , Group by activity status and personnel status :
The results of this query include and is_active
,is_staff
And the number of users in each group .
How to group by expression
GROUP BY Another common use case for is grouping by expression . for example , Calculate the number of users added each year :
Please note that , From us <field>__year
Use the date of the special expression to get the year at the first call values()
. The result of the query is a dict, The name of the key is date_joined__year
.
Sometimes built-in expressions are not enough , You need to summarize more complex expressions . for example , Group users who have logged in after registration :
The expression here is quite complicated . We use annotate
To build expressions , Then by referencing the expression in the call to the following pair , Mark it as GROUP BY key values()
. So let's start here , It's exactly the same .
How to use conditional aggregation
Use conditional aggregation , You can only aggregate part of a group . When you have multiple aggregations , Conditions will come in handy . for example , Calculate the number of employees and non staff users according to the year of signing :
above SQL Statements from PostgreSQL, It is associated with SQLite Together is the only support at present FILTER
Syntax shortcuts ( Officially known as “ Selective polymerization ”) The back end of the database . For other database backend ,ORM take CASE ... WHEN
Instead of using .
How to use Having Filter the aggregation results
The HAVING
Used to filter the results of aggregate functions , For example, find the year in which 100 More than users have registered :
How to press distinct grouping
For some aggregate functions , such as COUNT
, Sometimes you want to count only different events . For example, count the number of different surnames in each user's activity status :
How to use aggregate fields to create expressions
Aggregating fields is usually just the first step in a larger problem . For example, unique last name by user activity status percentage How much is the :
first annotate()
Define aggregate fields . The second kind annotate()
Use aggregate functions to construct expressions .
How to group through various relationships
up to now , We only use data in a single model , But aggregation is often used across relationships . A simpler solution is a one-to-one or foreign key relationship . for example , Suppose we UserProfile
And User There is a one-to-one relationship between , And we want to count users by profile type :
It's like GROUP BY The expression is the same , stay values
Relationships used in will be grouped by this field . Please note that , The name of the user profile type in the result will be “ user_profile__type”.
How to group by many to many relationship
A more complex type of relationship is many to many relationship . For example, calculate how many groups each user is a member of :
A user can be a member of multiple groups . To calculate the number of groups the user belongs to , We are User
Relevant names are used in the model “ Group ” . If the relevant name is not explicitly set (related_name), be Django It will automatically use format Generate name {related model model}_set
. for example ,group_set
.
Link to the original text :https://hakibenita.com/django-group-by-sql
original :KhakiBenita
translate : Big river dog
Related reading
Django QuerySet Query basis and skills . With her , Don't worry about SQL Yes .
Django Interacting with the database , You need to know 9 Tips
I hope I can understand it earlier Django Performance optimization techniques
Django Basics (24): aggregate and annotate Method use details and examples
边栏推荐
- 可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
- Which securities company is better to open an account? Is online account opening safe
- 玩转gRPC—深入概念与原理
- ApacheCN 翻译、校对、笔记整理活动进度公告 2022.7
- Huawei Nova 10 series released Huawei application market to build a solid application security firewall
- From repvgg to mobileone, including mobileone code
- 国产数据库乱象
- 迷失在Mysql的锁世界
- 什么是商业智能(BI),就看这篇文章足够了
- Common shortcut keys for hbuilder x
猜你喜欢
KDD2022 | 什么特征进行交互才是有效的?
并发优化总结
Machine learning notes mutual information
做BI开发,为什么一定要熟悉行业和企业业务?
LOGO特训营 第一节 鉴别Logo与Logo设计思路
[advanced C language] array & pointer & array written test questions
傳智教育|如何轉行互聯網高薪崗比特之一的軟件測試?(附軟件測試學習路線圖)
El tree combined with El table, tree adding and modifying operations
PMO:比较25种分子优化方法的样本效率
使用 BlocConsumer 同时构建响应式组件和监听状态
随机推荐
机器人相关课程考核材料归档实施细则2022版本
New intersectionobserver usage notes
服装企业为什么要谈信息化?
Force buckle 3_ 383. Ransom letter
开户哪家券商比较好?网上开户安全吗
现在mysql cdc2.1版本在解析值为0000-00-00 00:00:00的datetime类
【米哈游2023届秋招】开启【校招唯一专属内推码EYTUC】
2022-07-04:以下go语言代码输出什么?A:true;B:false;C:编译错误。 package main import “fmt“ func main() { fmt.Pri
Scala download and configuration
《命令行上的数据科学第二版》校对活动重新启动
B站大量虚拟主播被集体强制退款:收入蒸发,还倒欠B站;乔布斯被追授美国总统自由勋章;Grafana 9 发布|极客头条
vim 从嫌弃到依赖(23)——最后的闲扯
面试题 01.08. 零矩阵
HDU - 2859 Phalanx(DP)
What is business intelligence (BI), just look at this article is enough
The drawing method of side-by-side diagram, multi row and multi column
Why do you have to be familiar with industry and enterprise business when doing Bi development?
You don't have to run away to delete the library! Detailed MySQL data recovery
将QA引入软件开发生命周期是工程师要遵循的最佳实践
并发网络模块化 读书笔记转