SQL 计数去重
在了解带有DISTINCT关键字的Count函数的概念之前,我们必须先了解Count和Distinct关键字。所以,让我们从Count函数开始。
SQL中的Count是什么?
COUNT是结构化查询语言(SQL)中的一种函数,它显示结果中表中记录的数量。在SQL中,它通常在SELECT查询中使用。
Count函数的语法如下:
SELECT COUNT(Name_of_Column) FROM Name_of_Table;
在count示例中,我们必须在COUNT关键字之后的括号中定义列的名称。
Count函数示例
首先,我们必须创建一个新的表来执行计数函数。
下面的查询使用CREATE TABLE语句创建了 Teacher_Details 表,并以 Teacher_ID 作为主键:
CREATE TABLE Teacher_Details
(
Teacher_ID INT NOT NULL,
Teacher_Name varchar(100),
Teacher_Qualification varchar(50),
Teacher_Age INT,
Teacher_Interview_Marks INT
);
以下SQL查询使用INSERT INTO语句将新教师的记录插入上述表中:
INSERT INTO Teacher_Details VALUES (101, Anuj, B.tech, 20, 88);
INSERT INTO Teacher_Details VALUES (102, Raman, MCA, 24, NULL);
INSERT INTO Teacher_Details VALUES (104, Shyam, BBA, 19, 92);
INSERT INTO Teacher_Details VALUES (107, Vikash, B.tech, 20, NULL);
INSERT INTO Teacher_Details VALUES (111, Monu, MBA, 21, NULL);
INSERT INTO Teacher_Details VALUES (114, Jones, B.tech, 18, 93);
INSERT INTO Teacher_Details VALUES (121, Parul, BCA, 20, 97);
INSERT INTO Teacher_Details VALUES (123, Divya, B.tech, 21, NULL);
INSERT INTO Teacher_Details VALUES (128, Hemant, MBA, 23, 90);
INSERT INTO Teacher_Details VALUES (130, Nidhi, BBA, 20, 88);
INSERT INTO Teacher_Details VALUES (132, Priya, MBA, 22, NULL);
INSERT INTO Teacher_Details VALUES (138, Mohit, MCA, 21, 92);
让我们使用以下SELECT语句查看上表的记录:
SELECT * FROM Teacher_Details;
Teacher_ID | Teacher_Name | Teacher_Qualification | Teacher_Age | Teacher_Interview_Marks |
---|---|---|---|---|
101 | Anuj | B.tech | 20 | 88 |
102 | Raman | MCA | 24 | NULL |
104 | Shyam | BBA | 19 | 92 |
107 | Vikash | B.tech | 20 | NULL |
111 | Monu | MBA | 21 | NULL |
114 | Jones | B.tech | 18 | 93 |
121 | Parul | BCA | 20 | 97 |
123 | Divya | B.tech | 21 | NULL |
128 | Hemant | MBA | 23 | 90 |
130 | Nidhi | BBA | 20 | 88 |
132 | Priya | MBA | 22 | NULL |
138 | Mohit | MCA | 21 | 92 |
以下查询语句计算了Teacher_Details表中Teacher_Age列的总值:
SELECT COUNT (Teacher_Age) AS Total_Teachers_Age_Column FROM Teacher_Details;
输出:
以上SELECT查询的输出结果为12,因为 Teacher_Age 字段没有包含任何NULL值。
以下查询统计了上表中Teacher_Interview_Column的总值:
SELECT COUNT (Teacher_Interview_Marks) AS Total_Teachers_Interview_Marks FROM Teacher_Details;
此查询将在屏幕上显示如下输出:
上述SELECT查询的输出是7,因为Teacher_Interview_Marks列中的两个单元格包含NULL。而这五个NULL值被排除在外。所以SELECT查询结果显示7而不是12。
Count(*)
函数是什么?
这与Count函数类似,但唯一的区别是它还会显示表中NULL值的数量。
Count (*)
函数的语法如下所示:
SELECT COUNT(*) FROM Name_of_Table;
例子:
让我们来看一下上面的教师详情:
Teacher_ID | Teacher_Name | Teacher_Qualification | Teacher_Age | Teacher_Interview_Marks |
---|---|---|---|---|
101 | Anuj | B.tech | 20 | 88 |
102 | Raman | MCA | 24 | NULL |
104 | Shyam | BBA | 19 | 92 |
107 | Vikash | B.tech | 20 | NULL |
111 | Monu | MBA | 21 | NULL |
114 | Jones | B.tech | 18 | 93 |
121 | Parul | BCA | 20 | 97 |
123 | Divya | B.tech | 21 | NULL |
128 | Hemant | MBA | 23 | 90 |
130 | Nidhi | BBA | 20 | 88 |
132 | Priya | MBA | 22 | NULL |
138 | Mohit | MCA | 21 | 92 |
以下查询计算上表中 Total_Interview_Marks 列的总值:
SELECT COUNT (*) FROM Teacher_Details;
上面的带有COUNT(*)的SELECT查询将在屏幕上给出以下结果:
什么是SQL中的DISTINCT?
DISTINCT关键字显示结果中表中列的唯一行。
DISTINCT关键字的语法如下所示:
SELECT DISTINCT Name_of_Column FROM Table_Name WHERE [condition];
在DISTINCT查询中,我们还可以在WHERE子句中定义条件以检索特定的值。
DISTINCT示例
首先,创建一个要运行Distinct关键字的新表。
以下查询使用CREATE TABLE语句创建 Bike_Details 表:
CREATE TABLE Bike_Details
(
Bike_Name varchar(100),
Bike_Model INT,
Bike_Color varchar(50),
Bike_Cost INT
);
以下SQL查询使用INSERT INTO语句将新自行车的记录插入到表中:
INSERT INTO Bike_Details VALUES (KTM DUKE, 2019, Black, 185000);
INSERT INTO Bike_Details VALUES (royal Infield, 2020, Black, 165000);
INSERT INTO Bike_Details VALUES (Pulsar, 2018, Red, 90000);
INSERT INTO Bike_Details VALUES (Apache, 2020, White, 85000);
INSERT INTO Bike_Details VALUES (Livo, 2018, Black, 80000);
INSERT INTO Bike_Details VALUES (KTM RC, 2020, Red, 195000);
上述表格的记录是通过以下SELECT查询显示的:
SELECT * FROM Bike_Details;
Bike_Name | Bike_Model | Bike_Color | Bike_Cost |
---|---|---|---|
KTM DUKE | 2019 | Black | 185000 |
Royal Enfield | 2020 | Black | 165000 |
Pulsar | 2018 | Red | 90000 |
Apache | 2020 | White | 85,000 |
Livo | 2018 | Black | 80,000 |
KTM RC | 2020 | Red | 195,000 |
表格: Bike_Details
下面的SQL查询列出了上述 Bike_Details 表中Color列的不同值:
SELECT DISTINCT Bike_Color FROM Bikes ;
输出:
正如我们可以看到,黑色、红色和白色是Bike_Color列中的三个不同的值。
带有DISTINCT关键字的Count函数
在SELECT查询中使用COUNT函数和DISTINCT关键字,可以显示表格中字段的唯一数据的数量。
带有DISTINCT关键字的Count函数的语法如下所示:
SELECT COUNT(DISTINCT (Column_Name) FROM table_name WHERE [condition];
Count函数与DISTINCT关键字的示例
以下两个SQL示例将解释使用DISTINCT关键字的Count函数的执行过程:
示例1:
以下查询创建了一个具有四个字段的College_Students表:
CREATE TABLE College_Students
(
Student_Id INT NOT NULL,
Student_Name Varchar (40),
Student_Age INT,
Student_Marks INT
);
以下是将学生的记录插入到College_Students表中的INSERT查询:
INSERT INTO College_Students (Student_Id, Student_Name, Student_Age, Student_Marks) VALUES (101, Akhil, 28, 95),
(102, Abhay, 27, 86),
(103, Sorya, 26, 79),
(104, Abhishek, 27, 66),
(105, Ritik, 26, 79),
(106, Yash, 29, 88);
以下查询显示了College_Students表的详细信息:
SELECT * FROM College_Students;
Student_Id | Student_Name | Student_Age | Student_Marks |
---|---|---|---|
101 | Akhil | 28 | 95 |
102 | Abhay | 27 | 86 |
103 | Sorya | 26 | 79 |
104 | Abhishek | 27 | 66 |
105 | Ritik | 26 | 79 |
106 | Yash | 29 | 88 |
表格:College_Students
下面的SQL语句计算了College_Students表中Student_Age列的唯一值数量:
SELECT COUNT (DISTINCT (Student_Age) AS Unique_Age FROM College_Students ;
此查询将在输出中产生以下表格:
输出显示了四个值,因为Teacher_age列包含4个唯一的值。
示例2:
以下查询创建了具有四个字段的IT_Employee表:
CREATE TABLE IT_Employee
(
Employee_Id INT NOT NULL,
Employee_Name Varchar (40),
Emp_Age INT,
Employee_Salary INT
);
以下INSERT查询将IT员工的记录插入IT_Employee表中:
INSERT INTO IT_Employee (Employee_Id, Employee_Name, Employee_Age, Employee_Salary) VALUES (101, Akhil, 28, 25000),
(102, Abhay, 27, 26000),
(103, Sorya, 26, 29000),
(104, Abhishek, 27, 26000),
(105, Ritik, 26, 29000),
(106, Yash, 29, 25000);
以下查询显示了IT_Employee表的详细信息:
SELECT * FROM IT_Employee;
Employee_Id | Employee_Name | Employee_Age | Employee_Salary |
---|---|---|---|
101 | Akhil | 28 | 25000 |
102 | Abhay | 27 | 26000 |
103 | Sorya | 26 | 29000 |
104 | Abhishek | 27 | 26000 |
105 | Ritik | 26 | 29000 |
106 | Yash | 29 | 25000 |
表格: IT_Employee
以下SQL语句仅统计上述IT_Employee表中Emp_Age列的唯一值:
SELECT COUNT (DISTINCT (Employee_Age)) AS Unique_Age FROM IT_Employee ;
此查询将生成以下输出: