Checked some similar posts but they all had a common ID and these don’t so I’m hoping you ca help me.
What I want to achieve is this:
QTR1 | QTR2 | SUM(Sales) | SUM(Sales2)
1 2 9 8
1 3 9 7
1 4 9 6
2 1 8 9
2 3 8 7
And so on for all values to get a total of 12 results (Each quarter is compared to the three other quarters. The qtr1 and qtr2 are simply to distinguish the columns). I currently can do the product of just the quarters…
SELECT d1.quarter 'qtr1' , a.quarter 'qtr2'
FROM datedim d1, datedim a
WHERE a.quarter <> d1.quarter
GROUP BY a.quarter, d1.quarter;
But when I add in the sums for the quarters SUM(earnings) from the earnings table the query hangs and never completes. The earnings table is connected to the date table containing the quarters by a surrogate key, so ideally it has to be
FROM datedim, earningsfact
In order to get both data types and aggregate the sales.
Assuming I understand the problem correctly, here’s an example solution:
It assumes that you have quarterly data for multiple years. The common table expression summarizes it all into quarters, and the select statement compares each quarter to every other quarter.