I have two tables (a
and b
) which have both one date
field of type TIMESTAMP
. They represent two different type of actions (records): table a
's actions are $200 worth each, while table b
's ones worth $500.
I want to calculate the total count of dollars worth of both tables actions limiting the maximum number or dollars per day to $10.000. Is there a more clean or efficient way to do this other than:
SELECT SUM(daily_sum_total) AS total
FROM (
SELECT CASE WHEN SUM(daily_sum) > 10000 THEN 10000 ELSE SUM(daily_sum) END AS daily_sum_total, day
FROM (
SELECT COUNT(date) * 200 AS daily_sum, DATE(date) AS day
FROM a
GROUP BY day
UNION ALL
SELECT COUNT(date) * 500 AS daily_sum, DATE(date) AS day
FROM b
GROUP BY day
) AS daily_tmp_table
GROUP BY day
) as total_tmp_table;