Parse some details about SQL statement Count

Source: Internet
Author: User

The count Statement supports *, column name, constant, and variable, and can be modified with the distinct keyword, and count (column name) does not accumulate null records. Here are some examples to demonstrate the count rule: for example, to make statistics for the following table, all columns are represented by the SQL _variant type.

Copy codeThe Code is as follows:
If (object_id ('t_ test')> 0)
Drop table t_test
Go
Create table t_test (a SQL _variant, B SQL _variant, c SQL _variant)
Insert into t_test select 1, 1, 'A'
Insert into t_test select 1, getdate (), null
Insert into t_test select 'A', null, 1
Insert into t_test select 3, null, null
Insert into t_test select null, null, null
Go
Select * from t_test
Go
Select
Count (*) -- total
, Count (nullif (1, 1) -- returns 0 forever
, Count (a) -- a quantity
, Count (B) -- number of B
, Count (distinct a) -- a does not repeat the number
, Count (isnull (B, c) -- the number of B or c values not null
, Count (Coalesce (a, B, c) -- the number of a, B, or c is not null.
, Count (nullif (a, B) -- a is not equal to the number of B
, Count (nullif (isnumeric (cast (a as varchar (38), 0) -- a is the number of digits.
From t_test

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.