If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > How to write CASE in WHERE cause?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-07-03, 06:37
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
How to write CASE in WHERE cause?

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.

Last edited by grofaty; 10-07-03 at 06:44.
Reply With Quote
  #2 (permalink)  
Old 10-07-03, 07:56
blom0344 blom0344 is offline
Registered User
 
Join Date: Jan 2003
Location: Zutphen,Netherlands
Posts: 256
Grofaty:

Use temp sets like this (already tested it):

SELECT #SUM1-#SUM2 FROM
(Select ORGANIZATION_UNIT AS UNIT1,SUM(COST) AS #SUM1 from TARGET.GROFATY
WHERE YEAR = 2003
GROUP BY ORGANIZATION_UNIT) TEMP1,
(Select ORGANIZATION_UNIT AS UNIT2,SUM(COST) AS #SUM2 from TARGET.GROFATY
WHERE YEAR = 2001
GROUP BY ORGANIZATION_UNIT) TEMP2
WHERE TEMP1.UNIT1 = TEMP2.UNIT2
__________________
Ties Blom
Senior Application Developer BI
Getronics Healthcare
DB2,ORACLE,Powercenter,BusObj,Access,
SQL, SQL server
Reply With Quote
  #3 (permalink)  
Old 10-07-03, 14:14
chuzhoi chuzhoi is offline
Registered User
 
Join Date: Dec 2002
Posts: 134
You can use case in sum, it will save you one table/index scan

Select ORGANIZATION_UNIT,
SUM(case year when 2003 then COST end) -
SUM(case year when 2001 then COST end)
from TARGET.GROFATY
WHERE YEAR in (2003, 2001)
GROUP ORGANIZATION_UNIT
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On