A series of statistical functions are added in the background, and many situations require conditional counting. I have tried several methods. The following is a brief record for your reference. The problem is described to make the discussion simple and easy to understand. I simplified the problem and removed many background information. Once upon a time there was an emperor who had 50 nephews who gave birth to him with no reason.
A series of statistical functions are added in the background, and many situations require conditional counting. I have tried several methods. The following is a brief record for your reference. The problem is described to make the discussion simple and easy to understand. I simplified the problem and removed many background information. Once upon a time there was an emperor who had 50 nephews who gave birth to him with no reason.
A series of statistical functions are added in the background, and many situations require conditional counting. I have tried several methods. The following is a brief record for your reference.
Problem description
In order to make the discussion simple and easy to understand, I simplified the problem and removed many background information.
Once upon a time there was an emperor who had 50 nephews who gave birth to 100,000 sons without any reason. Therefore, the emperor was very upset, and it was difficult to manage a large number of sons, he wants to know how many sons each feicier has given him to reward him. This is hard to do. Therefore, the emperor invited a programmer to compile a program for him, and used a database to store the information of all his sons. In this way, he could use the program for statistics and management.
The database structure is as follows:
| Id |
Unique Emperor ID |
| Mother |
Unique ID of the Emperor's mother |
The Emperor divided the Emperor into two levels: tiangongniang (number less than 25) and tiangniang (number greater than or equal to 25 ), he wants to know the fertility strength of the tiangu Niang and the tiangu Niang. As a result, programmers began to write SQL queries.
Method 1: Use group bysql Query
- Select count (*) FROM 'Prince' group by 'Mother '> 24;
Execution result
- Count (*)
- 50029
- 49971
Running time on 100,000 rows of data: 0.0335 seconds
Analysis
The biggest problem with this group by method is that the results cannot be distinguished. Which of the two figures is the number of emperors generated by the mother-in-law of the tiangu, and which one is the number of emperors generated by the mother-in-law of the tiangu? I don't know. Therefore, although it counts the total number, it makes no sense.
Therefore, in order to differentiate statistical results, the condition mother> 24 must also be used as a field in the result set as a field. The modified SQL statement is as follows:
- Select count (*) AS 'number', 'Mother '> 24 AS 'type' FROM 'Prince 'group BY 'Mother'> 24;
Execution result
- Number type
- 50029 0
- 49971 1
When a condition expression is used as a field, the value of this field is the value of this condition expression. Therefore, in our example, type = 1 indicates that the value of mother> 24 is 1. Therefore, the numbers in the second row represent the number of emperors of the mother-in-law.
After modification, we can see that tiangu Niang is better than others.
Advantages and disadvantages
The disadvantage is obvious. Because conditional expressions are used as the grouping basis, they can only be used for binary division, but cannot be used for statistics in different categories. For example, to calculate 1 ~ 10, 11 ~ 24, 25 ~ The number of sub-accounts of the 50 th generation cannot be achieved.
In addition, because group by is used, sorting is involved and the execution time is longer.
I have not found any advantages of this method.
Method 2: Use nested SELECT
The nested SELECT statement can also be used to collect data under one condition in each SELECT clause, and then integrate the statistical data with a primary SELECT statement.
SQL Query
- SELECT
- (Select count (*) FROM 'Prince 'where 'Mother '> 24) AS 'digong ',
- (Select count (*) FROM 'Prince 'where 'Mother '<= 24) AS 'tiangong'
Execution result
- Digong tiangong
- 49971 50029
Running time on 100,000 rows of data: 0.0216 seconds
Analysis
This nested SELECT method is very intuitive, that is, to calculate the values under each condition separately, and finally to summarize, easy to understand, there is no difference with natural language.
Advantages and disadvantages
The advantage is that it is more intuitive and faster than group. Although there are three SELECT statements, it seems that there are two more statements than the group by solution, but it does not involve sorting, which saves a lot of time.
The disadvantage may be that the number of statements is slightly higher, and the number of statements may be uncomfortable.
Method 3: Use CASE WHEN
The case when statement is powerful and can define flexible query conditions. It is suitable for classification statistics.
SQL Query
- SELECT
- COUNT (case when 'Mother '> 24 THEN 1 else null end) AS 'digong ',
- COUNT (case when 'Mother '<= 24 THEN 1 else null end) AS 'tiangong'
- FROM prince
Execution result
- Digong tiangong
- 49971 50029
Running time on 100,000 rows of data: 0.02365825 seconds
Analysis
The key to this method is
- COUNT (case when 'Mother '> 24 THEN 1 else null end)
Here the COUNT and case when are used together to achieve the classification COUNT. Use case when first. WHEN the condition is met, set the field value to 1. If the condition is not met, set the field value to NULL. Then the COUNT function only counts non-NULL fields, therefore, the problem is solved.
Advantages and disadvantages
This method does not involve sorting, so the running time is equivalent to method 2, and the SELECT statement is reduced to one.
The disadvantage is that the statement is relatively long, and it may be uncomfortable for those who have cleanliness on the statement length.
Summary
Group by can be avoided to avoid sorting actions and accelerate Query execution.
If the value of a field needs to be classified, and the value of this field is variable, for example, the emperor wants to count the number of sub-accounts of each sub-account, and he may keep marrying many sub-accounts, in this case, the use of method 2 and method 3 is not very good, or a group by is easy and convenient.