Quantcast
Channel: dBforums – Everything on Databases, Design, Developers and Administrators
Viewing all articles
Browse latest Browse all 13329

Performing calculations on sums unioned together

$
0
0
I have a query that works perfectly, I get the exact values I need, except I need to add another column.


The top union provides the current month and future 12 months, where the bottom union provides the past 12 months.


Here is the select statement, leaving out the tables and joins:

SELECT sales_date as Sales_Date,
SUM (proj_on_hand * xx_conso_cost) as Projected_on_hand,
SUM((xx_sales_fcst + xx_trend_fcst)*xx_consol_cost) as Cost_of_FCST
Group by sales_date

union

SELECT sales_date as Sales_Date,
(SUM(xx_prod_inv))*1000 as Projected_on_hand,
(SUM(xx_prod_cost))*1000 as Cost_of_FCST
group by sales_date


The select statement returns results like the following:
Code:

Month    -    Projected_on_hand            -          Cost_of_FCST
Oct 12  -            10                    -            3
Nov 12  -            8                      -            5
Dec 12  -            9                    -            4
Jan 12  -            11                    -            6
etc


I want to keep displaying the month, projected_on_hand, and cost_of_fcst, but I also want to display an additional column.

I want to the sum the previous 3 months of cost_of_fcst and divide them by the sum of the previous 3 months of projected on_hand.

So the new column for Jan 12 should be .44 because (3+5+4)/(10+8+9)
The new column for Feb 12 should be .53 because (5+4+6)/(8+9+11)


I am in an oracle database environment, can I do this using a cursor? Any idea on how I would go about that?

Or do I have to go about creating a global temporary table?

Viewing all articles
Browse latest Browse all 13329

Trending Articles