Statistical functions of SQL
sql统计函数有 count 统计条数,配合group用 sum 累加指定字段数值但注意sum(1)就特殊
SUM (1) equals count (*)
sum(1)统计个数,功能和count(*)一样,但效率上count(*)高。所以尽量少用。
Give me a little example.
SELECT ad_network_id,,sum(1),count(*),sum(2),count(5)from mapping_table_analyticsGROUP BY ad_network_id
The result of the operation is:
3 123 123 123 2465 38 38 38 76
You can see that sum (1), COUNT (1), COUNT (2), COUNT (*) are used to count the numbers, and the results are the same.
Also, they all contain records of NULL values
The more special is sum (2), the total number of statistical results multiplied by 2.
(Note count (N) does not, and count (1) effect)
For example, SELECT sum (2) from mapping_table_analytics, the result is twice times the actual number of bars
Similarly, sum (n) is n times
I understand that the execution of sum (n) is to traverse the entire table, have a record, execute the N operation once, and return the aggregated overall result. So it's N times.
Statistics count wants to filter for NULL records
必须count(字段名):只有指定字段,才能过滤掉该字段值为NULL的记录
SELECT ad_network_id,sum(1),count(*),sum(2),count(5),count(id),count(type)from mapping_table_analyticsGROUP BY ad_network_id
The odd sum in SQL (1), SUM (2), COUNT (1), COUNT (6), COUNT (*): Total statistics