I'm attempting to create an SQL query that retrieves the total_cost
for every row in a table. Alongside that, I also need to collect the most dominant value for both columnA
and columnB
, with their respective values.
For example, with the following table contents:
cost | columnA | columnB | target |
---|---|---|---|
250 | Foo | Bar | XYZ |
200 | Foo | Bar | XYZ |
150 | Bar | Bar | ABC |
250 | Foo | Bar | ABC |
The result would need to be:
total_cost | columnA_dominant | columnB_dominant | columnA_value | columnB_value |
---|---|---|---|---|
850 | Foo | Bar | 250 | 400 |
Now I can get as far as calculating the total cost - that's no issue. I can also get the most dominant value for columnA
using this answer. But after this, I'm not sure how to also get the dominant value for columnB
and the values too.
This is my current SQL:
SELECT
SUM(`cost`) AS `total_cost`,
COUNT(`columnA`) AS `columnA_dominant`
FROM `table`
GROUP BY `columnA_dominant`
ORDER BY `columnA_dominant` DESC
WHERE `target` = "ABC"
UPDATE: Thanks to @Barmar for the idea of using a subquery, I managed to get the dominant values for columnA
and columnB
:
SELECT
-- Retrieve total cost.
SUM(`cost`) AS `total_cost`,
-- Get dominant values.
(
SELECT `columnA`
FROM `table`
GROUP BY `columnA`
ORDER BY COUNT(*) DESC
LIMIT 1
) AS `columnA_dominant`,
(
SELECT `columnB`
FROM `table`
GROUP BY `columnB`
ORDER BY COUNT(*) DESC
LIMIT 1
) AS `columnB_dominant`
FROM `table`
WHERE `target` = "XYZ"
However, I'm still having issues figuring out how to calculate the respective values.