The odd sum in SQL (1), SUM (2), COUNT (1), COUNT (6), COUNT (*): Total statistics

Source: Internet
Author: User

Statistical functions of SQL
sql统计函数有    count 统计条数,配合group用    sum 累加指定字段数值但注意sum(1)就特殊
SUM (1) equals 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
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.