Home > Database > Mysql Tutorial > How to Count Unique Records for Each Group-Subgroup Combination in MySQL?

How to Count Unique Records for Each Group-Subgroup Combination in MySQL?

Linda Hamilton
Release: 2024-12-27 11:32:10
Original
484 people have browsed it

How to Count Unique Records for Each Group-Subgroup Combination in MySQL?

Combining Group By and Count for Unique Record Counts

In your MySQL database, you encounter a table with two columns, group and subGroup, and you intend to determine the count of unique records for each joined group-subgroup combination. Here's how to achieve this using SQL:

SELECT group, subGroup, COUNT(*)
FROM YourTable
GROUP BY group, subGroup;

This query leverages the COUNT(*) function to count the occurrences for each unique combination of group and subGroup fields. The GROUP BY clause groups the rows based on these fields, resulting in aggregated counts for each distinct pair.

Consider the sample data you provided:

Group SubGroup Count
grp-A sub-A 2
grp-A sub-B 1
grp-B sub-A 1
grp-B sub-B 2

The query will produce a result table with the expected counts you specified. Each row in the result represents a unique combination of group and subGroup, along with the total count of records where that specific pair appears.

The above is the detailed content of How to Count Unique Records for Each Group-Subgroup Combination in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template