mysqlsqlpentahopentaho-report-designer

Group by does not provide accurate grouping


Attempting to create outputs that match the following screenshot:

enter image description here

When I attempt the following query:

SELECT t.amount, d.DOMAIN_NAME,td.month_number
FROM transaction t 
JOIN transaction_date td ON t.trans_date_key = td.trans_date_key
JOIN domain d ON t.domain_key = d.domain_key
WHERE td.month_number =7
ORDER BY amount DESC;

I get the output of:

enter image description here

When I implement this query:

SELECT t.amount, d.DOMAIN_NAME,td.month_number
FROM transaction t 
JOIN transaction_date td ON t.trans_date_key = td.trans_date_key
JOIN domain d ON t.domain_key = d.domain_key
WHERE td.month_number =7
GROUP BY domain_name
ORDER BY amount DESC;

I get the output of:

enter image description here

Why is my grouping only performing accurately on a few of the domain names, but not others?


Solution

  • You are not using GROUP BY correctly. You would need to use an aggregate function to sum the amounts. On the other hand, all non-aggregated columns should be listed in the GROUP BY clause.

    Consider:

    SELECT SUM(t.amount) total_amount, d.domain_name, td.month_number
    FROM transaction t 
    INNER JOIN transaction_date td ON t.trans_date_key = td.trans_date_key
    INNER JOIN domain d ON t.domain_key = d.domain_key
    WHERE td.month_number = 7
    GROUP BY d.domain_name, td.month_number
    ORDER BY total_amount DESC;
    

    What happens with the way you used GROUP BY is that MySQL actually picks a random record out of those that have the same domain_name. On most other RDBMS (and in non-ancient versions of MySQL), this would have generated a syntax error.