Can someone please help me with a SELECT?
Posted in 1998
Can someone help me with a SELECT?
I have a SELECT
SELECT YEAR, CUSTOMER, PRODUCT, PERIOD, REGION, SALES from my_table
order by YEAR, CUSTOMER, PRODUCT, PERIOD, REGION
which works fine.
Now here is the hard bit, I want to add a "derived" column : ytd_sales
ytd_sales corresponds to the sum of the sales, for every combination of YEAR,
CUSTOMER, PRODUCT.
In other words I want to find the ytd_sales for the current YEAR, CUSTOMER,
PRODUCT regardless of the period and region and include it in the returned row.
It has to be pure SQL.
As an example, suppose I had only 4 rows in the table , the output would look
like :
YEAR CUSTOMER PRODUCT PERIOD REGION SALES YTD_SALES
97 1 1 11 1 2 10
97 1 1 11 2 3 10
97 1 1 12 1 4 10
97 1 1 12 2 1 10
I hope I've explained it well!
Any suggestions gratefully received!