sql计数

This article explores SQL Count Distinct operator for eliminating the duplicate rows in the result set.

本文探讨了SQL Count Distinct运算符,该运算符用于消除结果集中的重复行。

A developer needs to get data from a SQL table with multiple conditions. Sometimes, we want to get all rows in a table but eliminate the available NULL values. Suppose we want to get distinct customer records that have placed an order last year.

开发人员需要从具有多个条件SQL表中获取数据。 有时,我们希望获取表中的所有行,但要消除可用的NULL值。 假设我们要获得去年下订单的不同客户记录。

Let’s go ahead and have a quick overview of SQL Count Function.

让我们继续快速浏览一下SQL Count Function。

SQL计数功能 (SQL Count Function)

We use SQL Count aggregate function to get the number of rows in the output. Suppose we have a product table that holds records for all products sold by a company. We want to know the count of products sold during the last quarter. We can use SQL Count Function to return the number of rows in the specified condition.

我们使用SQL Count聚合函数来获取输出中的行数。 假设我们有一个产品表,其中包含公司出售的所有产品的记录。 我们想知道上一季度销售的产品数量。 我们可以使用SQL Count Function返回指定条件下的行数。

The syntax of the SQL COUNT function:
COUNT ([ALL | DISTINCT] expression);

SQL COUNT函数的语法
COUNT([ALL | DISTINCT]表达式);

By default, SQL Server Count Function uses All keyword. It means that SQL Server counts all records in a table. It also includes the rows having duplicate values as well.

默认情况下,SQL Server计数功能使用All关键字。 这意味着SQL Server对表中的所有记录进行计数。 它还包括具有重复值的行。

Let’s create a sample table and insert few records in it.

让我们创建一个示例表并在其中插入一些记录。

CREATE TABLE ##TestTable (Id int identity(1,1), Col1 char(1) NULL);
INSERT INTO ##TestTable VALUES ('A');
INSERT INTO ##TestTable VALUES ('A');
INSERT INTO ##TestTable VALUES ('B');
INSERT INTO ##TestTable VALUES ('B');
INSERT INTO ##TestTable VALUES (NULL);
INSERT INTO ##TestTable VALUES (NULL);

In this table, we have duplicate values and NULL values as well.

在此表中,我们还有重复的值和NULL值。

In the following screenshot, we can note that:

在以下屏幕截图中,我们可以注意到:

  • Count (*) includes duplicate values as well as NULL values 计数(*)包括重复值以及NULL值
  • Count (Col1) includes duplicate values but does not include NULL values 计数(Col1)包含重复值,但不包含NULL值

Suppose we want to know the distinct values available in the table. We can use SQL COUNT DISTINCT to do so.

假设我们想知道表中可用的不同值。 我们可以使用SQL COUNT DISTINCT来做到这一点。

Select count(DISTINCT COL1)
from ##TestTable

In the following output, we get only 2 rows. SQL COUNT Distinct does not eliminate duplicate and NULL values from the result set.

在以下输出中,我们仅获得2行。 SQL COUNT Distinct不会从结果集中消除重复值和NULL值。

Let’s look at another example. In this example, we have a location table that consists of two columns City and State.

让我们看另一个例子。 在此示例中,我们有一个位置表,其中包含两列“城市”和“州”。

CREATE TABLE Location
(City  VARCHAR(30), State VARCHAR(20)
);Insert into location values('Gurgaon','Haryana')
Insert into location values('Gurgaon','Rajasthan')
Insert into location values('Jaipur','Rajasthan')
Insert into location values('Jaipur','Haryana')

Now, execute the following query to find out a count of the distinct city from the table.

现在,执行以下查询以从表中找出不同城市的计数。

SELECT COUNT(DISTINCT(City))
FROM Location;

It returns the count of unique city count 2 (Gurgaon and Jaipur) from our result set.

它从结果集中返回唯一城市计数2(古尔冈和斋浦尔)的计数。

If we look at the data, we have similar city name present in a different state as well. The combination of city and state is unique, and we do not want that unique combination to be eliminated from the output.

如果我们查看数据,那么在不同的州也将出现相似的城市名称。 城市和州的组合是唯一的,我们不希望从输出中消除该唯一的组合。

We can use SQL DISTINCT function on a combination of columns as well. It checks for the combination of values and removes if the combination is not unique.

我们也可以对列的组合使用SQL DISTINCT函数。 它检查值的组合,并删除组合是否唯一。

SELECT DISTINCT City, State
FROM Location;

It does not remove the duplicate city names from the output because of a unique combination of values.

由于值的唯一组合,它不会从输出中删除重复的城市名称。

Let’s insert one more rows in the location table.

让我们在位置表中再插入一行。

Insert into location values('Gurgaon','Haryana')

We have 5 records in the location table. In the data, you can see we have one combination of city and state that is not unique.

我们在位置表中有5条记录。 在数据中,您可以看到我们具有唯一的城市和州的组合。

Rerun the SELECT DISTINCT function, and it should return only 4 rows this time.

重新运行SELECT DISTINCT函数,这一次它应该只返回4行。

We cannot use SQL COUNT DISTINCT function directly with the multiple columns. You get the following error message.

我们不能将SQL COUNT DISTINCT函数直接用于多个列。 您收到以下错误信息。

We can use a temporary table to get records from the SQL DISTINCT function and then use count(*) to check the row counts.

我们可以使用一个临时表从SQL DISTINCT函数获取记录,然后使用count(*)检查行数。

SELECT DISTINCT City, State
into #Temp
FROM Location;
Select count(*) from #Temp

We get the row count 4 in the output.

我们在输出中得到行数4。

If we use a combination of columns to get distinct values and any of the columns contain NULL values, it also becomes a unique combination for the SQL Server.

如果我们使用列的组合来获取不同的值,并且任何列包含NULL值,则它也将成为SQL Server的唯一组合。

To verify this, let’s insert more records in the location table. We did not specify any state in this query.

为了验证这一点,让我们在位置表中插入更多记录。 我们没有在此查询中指定任何状态。

Insert into location values('Gurgaon','')
Insert into location(city)values('Gurgaon')

Let’s look at the location table data.

让我们看一下位置表数据。

Re-run the query to get distinct rows from the location table.

重新运行查询以从位置表中获得不同的行。

SELECT   distinct City, State
FROM Location;

In the output, we can see it does not eliminate the combination of City and State with the blank or NULL values.

在输出中,我们可以看到它没有消除带有空白或NULL值的City和State的组合。

Similarly, you can see row count 6 with SQL COUNT DISTINCT function.

同样,您可以通过SQL COUNT DISTINCT函数看到行计数6。

SELECT COUNT,COUNT(*)和SQL COUNT之间的区别是不同的 (Difference between SELECT COUNT, COUNT(*) and SQL COUNT distinct)

COUNT

Count(*)

Count(Distinct)

It returns the total number of rows after satisfying conditions specified in the where clause.

It returns the total number of rows after satisfying conditions specified in the where clause.

It returns the distinct number of rows after satisfying conditions specified in the where clause.

It gives the counts of rows. It does not eliminate duplicate values.

It considers all rows regardless of any duplicate, NULL values.

It gives a distinct number of rows after eliminating NULL and duplicate values.

It eliminates the NULL values in the output.

It does not eliminate the NULL values in the output.

It eliminates the NULL values in the output.

计数

计数(*)

计数(不同)

满足where子句中指定的条件后,它将返回总行数。

满足where子句中指定的条件后,它将返回总行数。

满足where子句中指定的条件后,它将返回不同的行数。

它给出了行数。 它不会消除重复的值。

它将考虑所有行,而不考虑任何重复的NULL值。

在消除NULL和重复值之后,它给出了不同数量的行。

它消除了输出中的NULL值。

它不会消除输出中的NULL值。

它消除了输出中的NULL值。

SQL Count不同功能的执行计划 (Execution Plan of SQL Count distinct function)

Let’s look at the Actual Execution Plan of the SQL COUNT DISTINCT function. You need to enable the Actual Execution Plan from the SSMS Menu bar as shown below.

让我们看一下SQL COUNT DISTINCT函数的实际执行计划。 您需要从SSMS菜单栏中启用“实际执行计划”,如下所示。

Execute the query to get an Actual execution plan. In this execution plan, you can see top resource consuming operators:

执行查询以获取实际执行计划。 在此执行计划中,您可以看到资源消耗最大的运算符:

  • Sort (Distinct Sort) – Cost 78% 排序(非重复排序)–费用78%
  • Table Scan – Cost 22% 表扫描–成本22%

You can hover the mouse over the sort operator, and it opens a tool-tip with the operator details.

您可以将鼠标悬停在排序运算符上,它会打开一个带有该运算符详细信息的工具提示。

In the properties windows, also we get more details around the sort operator including memory allocation, statistics, and the number of rows.

在属性窗口中,我们还将获得有关排序运算符的更多详细信息,包括内存分配,统计信息和行数。

In a table with million records, SQL Count Distinct might cause performance issues because a distinct count operator is a costly operator in the actual execution plan.

在具有百万条记录的表中,SQL Count Distinct可能会导致性能问题,因为在实际执行计划中,独特的count运算符是代价昂贵​​的运算符。

SQL Server 2019 improves the performance of SQL COUNT DISTINCT operator using a new Approx_count_distinct function. This new function of SQL Server 2019 provides an approximate distinct count of the rows. There might be a slight difference in the SQL Count distinct and Approx_Count_distinct function output.

SQL Server 2019使用新的Approx_count_distinct函数提高了SQL COUNT DISTINCT运算符的性能。 SQL Server 2019的此新功能提供了大约不同的行数。 SQL Count区别和Approx_Count_distinct函数输出可能会稍有不同。

You can replace SQL COUNT DISTINCT with the keyword Approx_Count_distinct to use this function from SQL Server 2019.

您可以使用关键字Approx_Count_distinct替换SQL COUNT DISTINCT以从SQL Server 2019使用此功能。

SELECT APPROX_COUNT_DISTINCT(City)
FROM Location;

You can explore more on this function in The new SQL Server 2019 function Approx_Count_Distinct.

您可以在新SQL Server 2019函数Approx_Count_Distinct中进一步了解此函数。

结论
(Conclusion
)

In this article, we explored the SQL COUNT Function with various examples. We also covered new SQL function Approx_Count_distinct available from SQL Server 2019. I would suggest reviewing them as per your environment. If you have any comments or questions, feel free to leave them in the comments below.

在本文中,我们通过各种示例探索了SQL COUNT函数。 我们还介绍了可从SQL Server 2019获得的新SQL函数Approx_Count_distinct 。 我建议根据您的环境对其进行审查。 如果您有任何意见或疑问,请随时将其留在下面的评论中。

翻译自: https://www.sqlshack.com/overview-of-sql-count-distinct-function/

sql计数

sql计数_SQL计数区分功能概述相关推荐

  1. sql示例_SQL Server Lead功能概述和示例

    sql示例 This article explores the SQL Server Lead function and its usage with various examples. 本文通过各种 ...

  2. sql server 群集_SQL Server群集索引概述

    sql server 群集 This article targets the beginners and gives an introduction of the clustered index in ...

  3. SQL Server中的登录触发器概述

    This article gives you an overview of Logon triggers in SQL Server and its usage to control SQL Serv ...

  4. 数据链路层功能概述----上

    目录 一.数据链路层功能概述 1.基本概念 2.数据链路层功能概述 功能一:为网络层提供服务: 功能二:链路管理, 功能三:组帧 功能四:流量控制 功能五:差错控制 3.透明传输 1.字符计数法: 2 ...

  5. 数据链路层功能概述、封装成帧与透明传输

    你一定要做自己,做自己喜欢的事,然后把自己交给命运 文章目录 本章启航思维导图 数据链路层 数据链路层基本概念 数据链路层功能概述 封装成帧 透明传输 组帧的四种方法 字符计数法 字符填充法 零比特填 ...

  6. 计算机网络之数据链路层功能概述、封装成帧、透明传输以及差错控制(检验编码)【408_1】

    一.数据链路层功能概述 (一)数据链路层基本概念 结点:主机.路由器 链路:网络中两个结点之间的物理通道,链路的传输介质主要有双绞线.光纤和微波.分为有线链路.无线链路. 数据链路:网络中两个结点之间 ...

  7. 5.1 计算机网络之传输层(传输层提供的服务及功能概述、端口、套接字--Socket、无连接UDP和面向连接TCP服务)

    文章目录 1.传输层提供的服务及功能概述 2.传输层的寻址与端口 (1)端口的作用 (2)端口号的分类 (3)套接字 3.无连接UDP和面向连接TCP服务 1.传输层提供的服务及功能概述 传输层的功能 ...

  8. java概述_Java 7功能概述

    java概述 前面我们讨论了所有未纳入Java 7的内容,然后回顾了将其纳入Java的有用的Fork / Join框架 . 今天的帖子将带我们了解Project Coin的每个功能-一系列小的语言增强 ...

  9. Java 7功能概述

    前面我们讨论了所有未纳入Java 7的内容,然后回顾了将其纳入Java 7的有用的Fork / Join框架 . 今天的帖子将带我们了解Project Coin的每个功能-一系列小的语言增强功能,这些 ...

最新文章

  1. 设计模式之“适配器模式”
  2. .Net(c#)加密解密之Aes和Des
  3. 搭建Eclipse C++开发环境
  4. Navicat使用教程:使用Navicat Premium 12自动执行数据库复制(四)
  5. 博士论文致谢走红后,黄国平母校演讲再刷屏!
  6. Nat Commun |完全开源!肿瘤基因组数据报告解读平台,可满足商业报告解决方案...
  7. 揭秘淘宝286亿海量图片存储与处理架构,互联网营销
  8. 红色Bootstrap自适应帝国cms7.5会员中心模板
  9. C++ 类模板三(类模版中的static关键字)
  10. WebGL多模型光照综合实例
  11. 2.14.PHP7.1 狐教程-【PHP 静态类、静态方法、静态属性】
  12. thinkphp5 图片压缩旋转_26套经典压缩机结构设计:有多少人知道内部结构是这样的...
  13. VB实际读写INI文件
  14. ffmpeg和JavaCV
  15. 什么是Windows安全模式?Windows安全模式详解
  16. 什么是cc攻击以及个人网站遭到cc攻击的解决方法
  17. True Liars (POJ - 1417)带权并查集+dp路径
  18. 大学cad课要用计算机么,cad2010大学课程
  19. PIC 1508 TIM1的定时器门控的理解
  20. 普通人的2022春招总结(阿里、腾讯offer)

热门文章

  1. HDU - 1043 Eight (A*搜索)
  2. 使用jmeter做接口测试----柠檬不萌!
  3. Android深度探索-卷1第二章心得体会
  4. 使用Cygwin登录Raspberry PI
  5. ASP.NET AjaxPro的应用 .AjaxPro使用中“XXX未定义”的一种解决方法(转载的)
  6. oracle 942错误(exp imp 出问题的解决方案)
  7. C\C++的转义字符
  8. hibernate+spring 注解 对事务的一些信息 (还没有整理)
  9. SQL Server 2005导入导出存储过程
  10. python3读取本地_Python3 获取本机 IP