Odd sum (1), sum (2), count (1), count (6), count (*) in SQL: Total number of statistics, sumcount
SQL statistical functions
The SQL statistical function has count statistics, and uses sum to accumulate the specified field value with sum, but note that sum (1) is special.
Sum (1) is equivalent to count (*)
Sum (1) is the same as count (*), but the efficiency is higher than count. So try to use less.
For example
SELECT ad_network_id,,sum(1),count(*),sum(2),count(5)
from mapping_table_analytics
GROUP BY ad_network_id
The running result is:
3 123 123 123 246
5 38 38 38 76
We can see that sum (1), count (1), count (2), count (*) are used to calculate the number, and the results are the same.
They all contain records with NULL values.
The special one is sum (2), which will multiply the total number of statistical results by 2.
(Note that count (N) does not. It works the same as count (1)
For example, if SELECT sum (2) from mapping_table_analytics, the result is twice the actual number of entries.
Similarly, sum (N) is N times
I understand that the execution process of sum (N) is to traverse the entire table and execute the add N operation once with a record, and return the overall result of accumulation. So it is N times.
Count records to filter NULL records
Count (field name): Only a specified field can be used to filter records whose field value is NULL.
SELECT ad_network_id,sum(1),count(*),sum(2),count(5),count(id),count(type)from mapping_table_analyticsGROUP BY ad_network_id