Distinctive Row Aggregation in MySQL with sum()
SQL queries often involve aggregate functions like sum() to compute numerical summaries. When dealing with grouped data, it's essential to count each unique record only once to avoid inflated results.
Consider the following query:
<code class="sql">SELECT links.id, count(DISTINCT stats.id) as clicks, count(DISTINCT conversions.id) as conversions, sum(conversions.value) as conversion_value FROM links LEFT OUTER JOIN stats ON links.id = stats.parent_id LEFT OUTER JOIN conversions ON links.id = conversions.link_id GROUP BY links.id ORDER BY links.created desc;</code>
The goal is to count the number of distinct clicks, conversions, and sum the corresponding conversion values for each link. However, the sum(conversions.value) aggregation counts each conversion value multiple times due to the grouping.
To accurately calculate the sum for each unique conversion, we need to adjust the expression. Assuming that conversions.id is the primary key in the conversions table, we can leverage the relationship between the number of distinct conversions and the total number of records.
For each conversions.id, there is at most one links.id affected. Therefore, the sum of all conversion values for a distinct conversion can be computed by multiplying the actual sum(conversions.value) by the number of distinct conversions and dividing it by the total number of records in the group:
<code class="sql">sum(conversions.value) * count(DISTINCT conversions.id) / count(*)</code>
Incorporating this adjustment into the query results in:
<code class="sql">SELECT links.id, count(DISTINCT stats.id) as clicks, count(DISTINCT conversions.id) as conversions, sum(conversions.value)*count(DISTINCT conversions.id)/count(*) as conversion_value FROM links LEFT OUTER JOIN stats ON links.id = stats.parent_id LEFT OUTER JOIN conversions ON links.id = conversions.link_id GROUP BY links.id ORDER BY links.created desc;</code>
This modified query effectively sums the conversion values for each unique conversion, providing accurate aggregate results for grouped data.
The above is the detailed content of How to Calculate Accurate Sum of Conversion Values for Distinct Conversions in MySQL?. For more information, please follow other related articles on the PHP Chinese website!