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!