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 > Oracle > How do i combine selected columns (AKA coding around bad data)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-16-06, 09:07
turk99 turk99 is offline
Registered User
 
Join Date: Jan 2004
Posts: 83
How do i combine selected columns (AKA coding around bad data)

I have a standard query, that returns head counts for units of my company. The problem is that "some" of the units should now be combined because the data I am using is legacy.

So while now I get something like this :

SELECT SUM(unit) as headcount, unit
FROM myTable

Unit / headcount
aa / 10
bb / 20
cc / 30
dd / 40

But I need to combine unit 'aa' and 'cc' (which i will rename as 'ac'). So my query should output this :

ac / 40
bb / 20
dd / 40

I essentially need to hard-code the combining rules in my query. Is this possible?
Reply With Quote
  #2 (permalink)  
Old 01-16-06, 09:09
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
Perhaps using DECODE or CASE?
Reply With Quote
  #3 (permalink)  
Old 01-16-06, 09:45
turk99 turk99 is offline
Registered User
 
Join Date: Jan 2004
Posts: 83
i haven't used decode or case before. i'm reading up on them now. but, in the examples i'm looking at it seems that i would have to hard code all my units, as opposed to only a few.

i don't want to have to do this :

case when unit = aa then 1
case when unit = bb then 1

i'd like to do something like "if unit = aa or cc, then combine, otherwise, count and group everything else"
Reply With Quote
  #4 (permalink)  
Old 01-16-06, 10:00
MCrowley MCrowley is online now
Wage drone 24601
 
Join Date: Jan 2003
Location: Massachusetts
Posts: 4,900
Thumbs up

Not sure if you are summing or counting units, but....
Code:
select sum(case when unit = 'aa' or unit = 'cc' then unitheadcount
else unitheadcount end) as headcount, 
case when unit = 'aa' or unit = 'cc' then 'ac' 
else unit end as unit
FROM myTable
group by case when unit = 'aa' or unit = 'cc' then 'ac' 
else unit end

Last edited by MCrowley; 01-16-06 at 10:04.
Reply With Quote
  #5 (permalink)  
Old 01-16-06, 10:31
turk99 turk99 is offline
Registered User
 
Join Date: Jan 2004
Posts: 83
I see you are using GROUP BY CASE. do i have to have a case statement in my select statment, or could i just have my query as it is now, and then add a GROUP BY CASE...(which i couldn't get to work)
Reply With Quote
  #6 (permalink)  
Old 01-17-06, 00:06
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
Quote:
Originally Posted by turk99
... it seems that i would have to hard code all my units ...
Well, if you DON'T hard code those values, how is Oracle supposed to know that you want to combine 'aa' with 'cc'?
Reply With Quote
  #7 (permalink)  
Old 01-17-06, 03:39
WilliamR WilliamR is offline
Registered User
 
Join Date: Sep 2004
Location: London, UK
Posts: 564
You could reduce the number of times you have to repeat the CASE/DECODE expression with something like this (untested):

Code:
SELECT unit
     , SUM(unitheadcount)
FROM   ( SELECT CASE
                   WHEN unit IN('aa','cc') THEN 'ac'
                   ELSE unit
                END AS unit
              , unitheadcount
         FROM   mytable )
GROUP BY unit;
Reply With Quote
  #8 (permalink)  
Old 01-18-06, 13:41
turk99 turk99 is offline
Registered User
 
Join Date: Jan 2004
Posts: 83
Quote:
Originally Posted by MCrowley
Not sure if you are summing or counting units, but....
Code:
select sum(case when unit = 'aa' or unit = 'cc' then unitheadcount
else unitheadcount end) as headcount, 
case when unit = 'aa' or unit = 'cc' then 'ac' 
else unit end as unit
FROM myTable
group by case when unit = 'aa' or unit = 'cc' then 'ac' 
else unit end
This was exactly what I needed. Thank you.
Reply With Quote
  #9 (permalink)  
Old 01-21-06, 10:07
WilliamR WilliamR is offline
Registered User
 
Join Date: Sep 2004
Location: London, UK
Posts: 564
Quote:
Originally Posted by MCrowley
Code:
case when unit = 'aa' or unit = 'cc' then unitheadcount
else unitheadcount end
Isn't that the same as
Code:
unitheadcount
?
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