I have a turnover table on the one side that has :
Storeid Turnover myDate
| 1 | 1000 | 2020-01-01 |
| 1 | 200 | 2020-01-02 |
| 1 | 4000 | 2020-01-03 |
| 1 | 1000 | 2020-01-05 |
on the other side I have a table with the number of transactions:
Storeid Transactions myDate
| 1 | 20 | 2020-01-01 |
| 1 | 40 | 2020-01-03 |
| 1 | 20 | 2020-01-04 |
| 1 | 60 | 2020-01-05 |
I need to work out the sum of the turnover and the sum of the transactions for a given date range. However I might have missing dates on either one of the tables. If I sum them individually I get the correct answer for each but any sort of inner or left join and I get incomplete answers (as per below):
select sum(Turnover), sum(transactions) from TurnoverTable
left join TransactionTable on TurnoverTable.storeid = TransactionTable.storeid and
TurnoverTable.myDate = TransactionTable.myDate where TurnoverTable.myDate >= '2020-01-01'
This will produce a sum for Turnover of 6200 and for Transactions of 120 (20 is missing from the 2020-01-04 date as this date is not available in the Turnover table, therefore fails in the join).
Short of running 2 select sum queries, is there a way to run these sums?
Much appreciated.