I’m trying to do some report line chart graphs and find it easiest if I return one data row from my query for each column (date) of data that will appear in a line chart. The challenge is that I want more than one line.
Here is what I can do with a simplified example of data:
| DATE | SALES | LOCATION |
| 2012-01-07 | 500 | 1 |
| 2012-01-07 | 600 | 2 |
| 2012-01-14 | 700 | 1 |
| 2012-01-14 | 400 | 2 |
| 2012-01-21 | 450 | 1 |
| 2012-01-21 | 550 | 2 |
SELECT date, sum(sales) as SalesTotal1 FROM TABLE WHERE location = ‘1’ group by date
Which returns:
| DATE | SalesTotal1 |
| 2012-01-07 | 500 |
| 2012-01-14 | 700 |
| 2012-01-21 | 450 |
That’s fine if I just have one line in my graph but what I really want in more than one alias of the same column still grouped by date that would return this:
| DATE | SalesTotal1 | SalesTotal2 |
| 2012-01-07 | 500 | 600 |
| 2012-01-14 | 700 | 400 |
| 2012-01-21 | 450 | 550 |
Is this possible? Sub query? I’ve tried many things, thanks ~
You could do something like:
You’d have to add in as many columns as there are locations though, and if you have many locations it would be annoying. Perhaps you could consider doing the re-arranging on the code side (if you have many, many locations)?