Odd sum (1), sum (2), count (1), count (6), count (*) in SQL: Total number of statistics, sumcount

Source: Internet
Author: User

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


Related Article

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: info-contact@alibabacloud.com 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.