Hi,
I would like to write SQL witch will compare the costs by unit for year 2003 compared to the costs by unit for year 2001. I don't want to write it with 'union all' statement, I would like to write something like "CASE inside the WHERE cause", but I just cann't done it. I know that
WHERE COALESCE(organization_unit,'UNIT_A')='UNIT_A' generaly works (but not for my sample), but how can this be done with CASE statement.
Code:
Sample data:
ORGANIZATION_UNIT YEAR COSTS
----------------------------------------------
UNIT_A 2001 100
UNIT_A 2003 110
UNIT_B 2001 50
UNIT_B 2003 40
UNIT_C 2001 1000
UNIT_C 2003 1200
etc
Bellow is SQL I would like to rewrite. UNIT_x (in bold) are variables.
select
organization_unit,
sum(costs) / (select sum(costs) from table where year=2001 and organization_unit=
'UNIT_A'),
from table
where year=2003 and organization_unit = '
UNIT_A'
group by organization_unit
UNION ALL
select
organization_unit,
sum(costs) / (select sum(costs) from table where year=2001 and organization_unit='
UNIT_B'),
from table
where year=2003 and organization_unit = '
UNIT_B'
group by organization_unit
UNION ALL
etc.