I have a table which (simplified) has
CustomerID
PerdioNo (YYYYMM)
Amount
and therefore has multiple rows per CustomerID, if they have activity on several periods.
I would like to transform this into 1 row per CustomerID, with:
CustomerID, 1999Amount, 2000Amount, 2001Amount
I have been able to split the amounts into 3 columns accross, but not sum them to 1 row. I'm using:
select
address.CUSTOMER_ID,
CASE When SUBSTR(customer_period_totals.PERIOD_ID,1,4) = '1999' then (customer_period_totals.AMOUNT_1 - customer_period_totals.AMOUNT_2) END,
CASE When SUBSTR(customer_period_totals.PERIOD_ID,1,4) = '2000' then (customer_period_totals.AMOUNT_1 - customer_period_totals.AMOUNT_2) END,
CASE when substr(customer_period_totals.PERIOD_ID,1,4) = '2001' then (customer_period_totals.AMOUNT_1 - customer_period_totals.AMOUNT_2) END
from address, customer_period_totals
where address.CUSTOMER_ID = customer_period_totals.CUSTOMER_ID
order by address.customer_id
If I try to use the sum function inside the CASE statement (changing the ORDER BY to GROUP BY I get an 'ORA-00979: not a group by expression' just after the SUBSTR( in the case expression.
Any idea how I can do this??!!!
Many, many thaks,
Mark
London
CustomerID
PerdioNo (YYYYMM)
Amount
and therefore has multiple rows per CustomerID, if they have activity on several periods.
I would like to transform this into 1 row per CustomerID, with:
CustomerID, 1999Amount, 2000Amount, 2001Amount
I have been able to split the amounts into 3 columns accross, but not sum them to 1 row. I'm using:
select
address.CUSTOMER_ID,
CASE When SUBSTR(customer_period_totals.PERIOD_ID,1,4) = '1999' then (customer_period_totals.AMOUNT_1 - customer_period_totals.AMOUNT_2) END,
CASE When SUBSTR(customer_period_totals.PERIOD_ID,1,4) = '2000' then (customer_period_totals.AMOUNT_1 - customer_period_totals.AMOUNT_2) END,
CASE when substr(customer_period_totals.PERIOD_ID,1,4) = '2001' then (customer_period_totals.AMOUNT_1 - customer_period_totals.AMOUNT_2) END
from address, customer_period_totals
where address.CUSTOMER_ID = customer_period_totals.CUSTOMER_ID
order by address.customer_id
If I try to use the sum function inside the CASE statement (changing the ORDER BY to GROUP BY I get an 'ORA-00979: not a group by expression' just after the SUBSTR( in the case expression.
Any idea how I can do this??!!!
Many, many thaks,
Mark
London