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 > Problem with a

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-22-07, 05:04
PierreD PierreD is offline
Registered User
 
Join Date: Nov 2007
Posts: 3
Problem with a

Hi there,

I'm having problems with the following query, apparently in the CASE as system shows me the following error message :
''The search-condition in a SEARCH-WHEN-CLAUSE is not valid.''

SELECT SEC_COV_ID, PAR_SEC_PLG_FL,
(CASE WHEN OPS_RTG_AGT_ID ='UNR' THEN 'UNRATED'
WHEN OPS_RTG_ID IN ('A1','A2','A3','AAA','AA','A','BBB')
THEN 'INVESTMENT GRADE'
WHEN OPS_RTG_ID IN ('A4','A5','A6','BB','B','CCC','CC','C','D')
THEN 'NON INVESTMENT GRADE'
ELSE OPS_RTG_ID END) AS SECRTG,
SUM(PAR_SEC_POS_COL_AM), SUM(PAR_SEC_POS_MKV_AM),
SUM(PAR_SEC_POS_COL_AM)/SUM(PAR_SEC_POS_MKV_AM)
FROM PDB2I.DI_PAR_ACC_01C A,
PDB2I.RI_PAR_SEC_POS_01 B,
PDB2I.DI_OPR_01 C,
PDB2I.DI_SEC_STB_01 D
WHERE A.PAR_ACC_NB=B.PAR_ACC_NB
AND B.SEC_ID=D.SEC_ID
AND C.OPR_ID=A.PAR_ACC_NB
AND SEC_COV_ID IN ('CDO','ABS','CMBS','RMBS')
AND SEC_CAN_DT='0001-01-01'
AND SEC_HLD_FL='Y'
AND SEC_MAT_DT >CURRENT DATE
AND PAR_SEC_POS_TYP_AB='SB'
GROUP BY SEC_COV_ID, PAR_SEC_PLG_FL,
OPS_RTG_AGT_ID, OPS_RTG_ID

Many thanks in advance for providing me with the correct script!

Kind regards,

PierreD
Reply With Quote
  #2 (permalink)  
Old 11-22-07, 05:45
PierreD PierreD is offline
Registered User
 
Join Date: Nov 2007
Posts: 3
Resolution
(by the way, I can't believe that I have to write it down like this >_<)

SELECT SEC_COV_ID, PAR_SEC_PLG_FL,
(CASE WHEN OPS_RTG_AGT_ID ='UNR' THEN 'UNRATED'
WHEN OPS_RTG_ID = 'A1' OR OPS_RTG_ID ='A2'
OR OPS_RTG_ID ='A3'
OR OPS_RTG_ID ='AAA'
OR OPS_RTG_ID ='AA'
OR OPS_RTG_ID ='A'
OR OPS_RTG_ID ='BBB'
THEN 'INVESTMENT GRADE'
WHEN OPS_RTG_ID = 'A4' OR OPS_RTG_ID ='A5'
OR OPS_RTG_ID ='A6'
OR OPS_RTG_ID ='BB'
OR OPS_RTG_ID ='B'
OR OPS_RTG_ID ='CCC'
OR OPS_RTG_ID ='CC'
OR OPS_RTG_ID ='C'
OR OPS_RTG_ID ='D'
THEN 'NON INVESTMENT GRADE'
ELSE OPS_RTG_ID END) AS SECRTG,
SUM(PAR_SEC_POS_COL_AM), SUM(PAR_SEC_POS_MKV_AM),
SUM(PAR_SEC_POS_COL_AM)/SUM(PAR_SEC_POS_MKV_AM)
FROM PDB2I.DI_PAR_ACC_01C A,
PDB2I.RI_PAR_SEC_POS_01 B,
PDB2I.DI_OPR_01 C,
PDB2I.DI_SEC_STB_01 D
WHERE A.PAR_ACC_NB=B.PAR_ACC_NB
AND B.SEC_ID=D.SEC_ID
AND C.OPR_ID=A.PAR_ACC_NB
AND SEC_COV_ID IN ('CDO','ABS','CMBS','RMBS')
AND SEC_CAN_DT='0001-01-01'
AND SEC_HLD_FL='Y'
AND SEC_MAT_DT >CURRENT DATE
AND PAR_SEC_POS_TYP_AB='SB'
GROUP BY SEC_COV_ID, PAR_SEC_PLG_FL,
OPS_RTG_AGT_ID, OPS_RTG_ID

If you have an other way to structure the query, I would be glad to read it.

Thanks!
Reply With Quote
  #3 (permalink)  
Old 11-22-07, 06:28
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
High PierreD,

what's your DB2 version and system ?

Normally, CASE combined with
column IN ( value,value..)
should work.

What is the exact error message ?

I see a possible inconsistence (not matching columns) between your SELECT clause and your GROUP BY.

SELECT SEC_COV_ID, PAR_SEC_PLG_FL, (expression) AS SECRTG, SUM(),SUM(),SUM()
GROUP BY SEC_COV_ID, PAR_SEC_PLG_FL, OPS_RTG_AGT_ID, OPS_RTG_ID
Reply With Quote
  #4 (permalink)  
Old 11-22-07, 06:40
PierreD PierreD is offline
Registered User
 
Join Date: Nov 2007
Posts: 3
Hi,

I have no idea about version and system but the above query works now, it's just the fact that the 'WHEN' condition doesn't accept 'IN', so I had to find a way around and the list of 'OR' seems to be the only solution... :s

Concerning the grouping, I may not group by a user defined column i.e. SECRTG, so i had to group by what's in this user defined column i.e.
OPS_RTG_AGT_ID, OPS_RTG_ID

Thanks for having replied.
Reply With Quote
  #5 (permalink)  
Old 11-22-07, 09:57
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
You should really figure out which version of DB2 you are running on which platform. Because what you describe works perfectly fine (on DB2 LUW):
Code:
$ db2 "values case when 'a' IN ( '1', 'a', '1398712', 'oaoecg' ) then 1 end"

1
-----------
          1

  1 record(s) selected.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
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