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 > Query Help - SELECT - CASE Statement

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-19-09, 11:35
TopNotch TopNotch is offline
Registered User
 
Join Date: Mar 2009
Posts: 10
Question Query Help - SELECT - CASE Statement

The below query gives me M, A or B depending WHERE the COLA is present.
If present in TBLM --> gives M
present in TBLA --> gives A
present in TBLB --> gives B


SELECT CASE
WHEN A.COLA IS NOT NULL
AND B.COLA IS NOT NULL THEN 'M'
WHEN A.COLA IS NOT NULL THEN 'A'
WHEN B.COLA IS NOT NULL THEN 'B'
END
FROM TBLM
LEFT OUTER JOIN TBLA
ON A.COLA = M.COLA
LEFT OUTER JOIN TBLB
ON B.COLA = M.COLA

WHERE M.COLA = XXX

I need to convert the SQL to give me 'Y' when COLA is present in
1) TBLM and TBLA
OR
2) TBLM and TBLB
OR
3) TLM and TBLA and TBLB
Please advise..
Reply With Quote
  #2 (permalink)  
Old 04-19-09, 13:22
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
The phrase "is present" is not so clear for me.
Is that "NOT NULL"?
Or, a row was retrieved?

In either case, I couldn't understand the relationship of the followings.
Quote:
If present in TBLM --> gives M
and
Quote:
WHEN A.COLA IS NOT NULL
AND B.COLA IS NOT NULL THEN 'M'
Because, rows of TBLM are always retrieved if the predicate "M.COLA = XXX" was true,
regardless the rows of TBLA or TBLB were retrieved or not.

Anyhow, answer to your question might be:
WHEN A.COLA IS NOT NULL OR B.COLA IS NOT NULL THEN 'Y'
Reply With Quote
  #3 (permalink)  
Old 04-19-09, 22:52
TopNotch TopNotch is offline
Registered User
 
Join Date: Mar 2009
Posts: 10
Yes..I meant 'is present meaning NOT NULL'.

TBLM is NOT NULL
and also
either (TBLA or TBLB) is NOT NULL.

Sorry for confusion.
Reply With Quote
  #4 (permalink)  
Old 04-20-09, 00:51
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
'Y' when
1) TBLM.COLA IS NOT NULL and TBLA.COLA IS NOT NULL
OR
2) TBLM.COLA IS NOT NULL and TBLB.COLA IS NOT NULL
OR
3) TBLM.COLA IS NOT NULL and TBLA.COLA IS NOT NULL and TBLB.COLA IS NOT NULL

WHEN TBLA.COLA IS NOT NULL OR TBLB.COLA IS NOT NULL THEN 'Y'

Because, if TBLM.COLA IS NULL,
no row of TBLA nor TBLB will be joined then TBLA.COLA and TBLB.COLA are always NULL.

Last edited by tonkuma; 04-20-09 at 00:54.
Reply With Quote
  #5 (permalink)  
Old 04-20-09, 11:16
TopNotch TopNotch is offline
Registered User
 
Join Date: Mar 2009
Posts: 10
Yes..that works..many thanks.
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