I have a select statement that gathers data from one monthly-summary table like the following
companyname, employee, id, usage_a, ... usage_m
I need to create report from 6 monthly-summary tables that looks like the following
companyname, employee, id,
jan_usage_a, ...june_usage_a, average (jun_jan_usage_a),
...
jan_usage_m, ...june_usage_m, average (jan_jun_usage_m)
I have done something similar with less columns, but i had to create a table with columns for each month’s usage and then merge data in month to month with a single query. At the end I would export to excel create another column to average the each usage over the months required.
For the report above I’d have to create a table over one hundred columns.
My question is is there a better way to do this?
Thanks.
Sorry for the title if it’s a little unclear.
If you have six different tables, then you need to join them together:
The problem that you have is that the populations in the different months may be different, so the joins will lose rows. A good way to handle this is with a driving table:
You can do all this in one SQL statement. There are repetitive parts (such as the column names in the select). Consider using Excel to generate these.