I have a single table with some simple number i'd like to sum across different date ranges. Example:
date | cost | storeId | stateId |
-------------------------------------
1/1/2021 | 100 | 1 | 1 |
1/1/2020 | 30 | 2 | 2 |
I would like to sum the costs across different timespans, so sum of the costs of records within 1 month of now, 2 months of now, etc. I also have several conditions on which to count that require JOINs. This led me to creating a massive query that reuses the same joins and where clause (except the date range) and UNIONs:
SELECT SUM(cost)
FROM table1 t1
LEFT JOIN table2 t2 ON t2.id = t1.storeId
LEFT JOIN table3 t3 ON t3.id = t1.stateId
WHERE t1.date > '1monthAgo' t2.otherId = 1 AND t2.anotherId = 2 AND t3.someId = 1
UNION
SELECT SUM(cost)
FROM table1 t1
LEFT JOIN table2 t2 ON t2.id = t1.storeId
LEFT JOIN table3 t3 ON t3.id = t1.stateId
WHERE t1.date > '2monthsAgo' t2.otherId = 1 AND t2.anotherId = 2 AND t3.someId = 1
UNION
SELECT SUM(cost)
FROM table1 t1
LEFT JOIN table2 t2 ON t2.id = t1.storeId
LEFT JOIN table3 t3 ON t3.id = t1.stateId
WHERE t1.date > '3monthsAgo' t2.otherId = 1 AND t2.anotherId = 2 AND t3.someId = 1
In my real query, these joins and where clauses are much more complicated and make this query not just a mess, but very slow (~4 seconds). I tried re-writing this with CASE statements, and now have:
SELECT
SUM(CASE t1.date > '1monthAgo' THEN t1.cost ELSE 0 END),
SUM(CASE t1.date > '2monthsAgo' THEN t1.cost ELSE 0 END),
SUM(CASE t1.date > '3monthsAgo' THEN t1.cost ELSE 0 END),
FROM table1 t1
LEFT JOIN table2 t2 ON t2.id = t1.storeId
LEFT JOIN table3 t3 ON t3.id = t1.stateId
WHERE t2.otherId = 1 AND t2.anotherId = 2 AND t3.someId = 1
This presents the data slightly differently (all in one row) but it runs much faster (1 second) and doesn't duplicate the JOINs and WHERE clauses. However my real query has 4 different sums across 6 different date ranges, so there are 24 CASE statements and I do not think this is the right way to do this.
Is it possible to have a single query that would share the JOINs and WHERE clauses but allow me to query across different date ranges to gather sums?