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 > Multiple rows as a result of case statement

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-11-09, 15:39
VA. VA. is offline
Registered User
 
Join Date: Dec 2009
Posts: 3
Multiple rows as a result of case statement

Hello,

Here's a scenario

table

Acct Prod Status
acct1 1 A
acct1 2 C
acct2 2 A
acct2 1 C


select distinct Acct, case when Prod = 1 then Status end as Prod 1, case when Prod = 2 then Status end as Prod 2
from table.......

I expect to have one row per Acct with 2 columns, one for each products

Acct Prod 1 Prod 2
acct1 A C
acct2 C A

but getting this

Acct Prod 1 Prod 2
acct1 A
acct1 C
acct2 C
acct2 A

this is driving me up the wall, I know this worked at my previous company with Oracle, but it doesn't here with DB2. Can somebody please help?!

Thanks,
VA
Reply With Quote
  #2 (permalink)  
Old 12-11-09, 15:40
VA. VA. is offline
Registered User
 
Join Date: Dec 2009
Posts: 3
Ooops, it got all messed up. Let me know if this doesn't make sense....
Reply With Quote
  #3 (permalink)  
Old 12-11-09, 16:54
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Multiple rows returns not by CASE (it's not a possible), but by search condition on WHERE clause.

Lenny
Reply With Quote
  #4 (permalink)  
Old 12-11-09, 21:10
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
To get:
Acct Prod 1 Prod 2
acct1 A C
acct2 C A

From:
table
Acct Prod Status
acct1 1 A
acct1 2 C
acct2 2 A
acct2 1 C


You can try this query:
Code:
SELECT Acct
     , MAX(CASE Prod WHEN 1 THEN Status END) AS Prod_1
     , MAX(CASE Prod WHEN 2 THEN Status END) AS Prod_2
  FROM table
 GROUP BY
       Acct
;
You may be able to get some hint by searching the forum with keyword "pivot/unpivot".
Reply With Quote
  #5 (permalink)  
Old 12-14-09, 13:54
VA. VA. is offline
Registered User
 
Join Date: Dec 2009
Posts: 3
Thanks for your input!
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