When using SQL to extract data, we often encounter duplicate values in the table. For example, if we want to get UV (unique visitors), we need to deduplicate. In MySQL, For example, there is a table task like this: Remark:
We need to find the total number of tasks. Since task_id is not unique, we need to remove duplicates: distinct -- List all unique values of task_id (after deduplication) select distinct task_id from Task; --Total number of tasks select count(distinct task_id) task_num from Task;
group by -- List all unique values of task_id (after deduplication, null is also a value) -- select task_id -- from Task -- group by task_id; --Total number of tasks select count(task_id) task_num from (select task_id from Task group by task_id) tmp; row_number row_number is a window function with the following syntax: -- Use select count(case when rn=1 then task_id else null end) task_num in SQL that supports window functions from (select task_id , row_number() over (partition by task_id order by start_time) rn from Task) tmp; In addition, let's use a table test to explain the use of distinct and group by in deduplication: -- The semicolon below is used to separate rows select distinct user_id from Test; -- returns 1; 2 select distinct user_id, user_type from Test; -- returns 1, 1; 1, 2; 2, 1 select user_id from Test group by user_id; -- returns 1; 2 select user_id, user_type from Test group by user_id, user_type; -- returns 1, 1; 1, 2; 2, 1 select user_id, user_type from Test group by user_id; -- Hive, Oracle, etc. will report an error, but MySQL can be written like this. -- Returns 1, 1 or 1, 2; 2, 1 (two rows in total). Only the fields after group by will be deduplicated, which means the number of records returned at the end is equal to the number of records in the previous SQL statement, that is, 2 records. For fields that are not placed after group by but are placed in select, only one record will be returned (usually the first one, but there should be no pattern). This is the end of this article on the summary of SQL deduplication methods. For more relevant SQL deduplication methods, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Share 10 of the latest web front-end frameworks (translation)
>>: Pure CSS to achieve hover image pop-out pop-up effect example code
Table of contents What is recursion and how does ...
Table of contents I. Overview 2. Conventional mul...
Table of contents Image capture through svg CSS p...
If there is a table product with a field add_time...
Well, you may be a design guru, or maybe that'...
For record, it may be used in the future, and fri...
【question】 We have an HP server. When the SSD wri...
By default, the table title is horizontally cente...
Table of contents 1. Docker configuration 2. Crea...
This post focuses on a super secret Flutter proje...
<br />Some web pages may not look large but ...
Overview The project was created successfully and...
This is the first time I used the CentOS7 system ...
1. Download Navicat for MySQL 15 https://www.navi...
Without further ado, let’s run the screenshot dir...