Re: Can someone please help me with a SELECT?
Posted in 1998
Belakimem wrote:
>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.
Ok, hope I understood it.
Select a.YEAR, a.CUSTOMER, a.PRODUCT, a.PERIOD,
a.REGION, a.SALES, (SELECT SUM(b.SALES)
FROM my_table b
WHERE b.YEAR = a.YEAR
AND b.CUSTOMER = a.CUSTOMER
AND b.PRODUCT = a.PRODUCT
)
From my_table a
Order by a.YEAR, a.CUSTOMER, a.PRODUCT, a.PERIOD, a.REGION
>
>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!
--
Octav Chiriac Phone: (373) 2 21 20 96
NetInfo S.R.L. Fax: (373) 2 21 20 96
Chisinau (373) 2 24 00 83
Moldova, Republic of mailto:com@netinfo.moldova.net