MySQL: Efficiently Aggregating Counts Across Multiple Columns within a Single Table
Multiple queries targeting the same table but different columns in MySQL can lead to inefficient data retrieval and inaccurate results. Let's illustrate this with an example and show the correct approach.
Suppose you need to calculate counts based on various criteria within the t_hospital
table:
Inefficient Method (Multiple Queries):
The following code attempts to achieve this using multiple queries, but it produces incorrect results for columns beyond the first:
<code class="language-sql">SET @start_res = 20150301; SET @finish_res= 20150501; SET @finish_check= 20150801; SET @start_check= 20150301; SET @daily_hos= 3; SELECT* from ( SELECT COUNT(DAY_IN) AS arr FROM t_hospital WHERE DAY_IN between @start_check and @finish_check and RES_DATE between @start_res and @finish_res and ID_daily_hos =@daily_hos group by DAY_IN )e, (SELECT COUNT(PAT_STATUS) AS ONG1 FROM t_hospital WHERE PAT_STATUS like '%ong%' and DAY_IN between @start_check and @finish_check and RES_DATE between @start_res and @finish_res and ID_daily_hos =@daily_hos group by DAY_IN ) a, (SELECT COUNT(PAT_STATUS) AS RTED FROM t_hospital WHERE PAT_STATUS like '%rtde%'and DAY_IN between @start_check and @finish_check and RES_DATE between @start_res and @finish_res and ID_daily_hos =@daily_hos group by DAY_IN )b, (SELECT COUNT(PAT_STATUS) AS POLI FROM t_hospital WHERE PAT_STATUS like '%pol%'and DAY_IN between @start_check and @finish_check and RES_DATE between @start_res and @finish_res and ID_daily_hos =@daily_hos group by DAY_IN )c, (SELECT COUNT(PAT_STATUS) AS para FROM t_hospital WHERE PAT_STATUS like '%para%' and DAY_IN between @start_check and @finish_check and RES_DATE between @start_res and @finish_res and ID_daily_hos =@daily_hos group by DAY_IN )d;</code>
Efficient Method (Single Query with Conditional Aggregation):
The optimal solution involves a single query employing conditional aggregation:
<code class="language-sql">SELECT DAY_IN, COUNT(*) AS arr, SUM(IF(PAT_STATUS like '%ong%', 1, 0)) AS ONG1, SUM(IF(PAT_STATUS like '%rtde%', 1, 0)) AS RTED, SUM(IF(PAT_STATUS like '%pol%', 1, 0)) AS POL1, SUM(IF(PAT_STATUS like '%para%', 1, 0)) AS para FROM t_hospital WHERE DAY_IN between @start_check and @finish_check and RES_DATE between @start_res and @finish_res and ID_daily_hos =@daily_hos GROUP BY DAY_IN;</code>
This approach utilizes the IF()
function to conditionally count occurrences based on PAT_STATUS
values, and SUM()
aggregates these counts for each DAY_IN
. This method is significantly more efficient and provides accurate results. It avoids the Cartesian product implicitly created by joining multiple subqueries.
The above is the detailed content of How to Efficiently Aggregate Multiple Counts from Different Columns in the Same MySQL Table?. For more information, please follow other related articles on the PHP Chinese website!