Quantcast
Channel: IBM Mainframe Computers Forums
Viewing all articles
Browse latest Browse all 8500

DB2 :: RE: SQL query to run through list of values in table

$
0
0
Author: Rohit Umarjikar
Posted: Thu Dec 15, 2016 10:12 pm (GMT 5.5)

I still don't get why Arun's query doesn't work for what you wanted except sorting. So let us know in what case his query will fail.
Please try this.
Code:
select t4.ID,
       t4.code
from
(SELECT
  ID,
  code,
  case
       when code = 'B' then 2
       when code = 'A' then 3
       when code = 'C' then 4 
       when code = 'D' then 5
  end
  FROM TABLE1 T1, TABLE2 T2
where
    t1.Merchant = t2.Merchant
AND EXISTS ( select  1 form table2 t3
         where t1.Merchant = t3.Merchant
            and t3.code in ('A','B','C','D'))
UNION
 SELECT
  ID,
  'X',
  1
  FROM TABLE1 T1, TABLE2 T2
where
    t1.Merchant = t2.Merchant
AND EXISTS ( select  1 form table2 t3
         where t1.Merchant = t3.Merchant
            and t3.code  not in ('A','B','C','D'))
AND NOT EXISTS ( select  1 form table2 t3
         where t1.Merchant = t3.Merchant
            and t3.code in ('A','B','C','D'))
order by 3) t4

_________________
Regards,
Rohit Umarjikar
"Knowledge is knowing that a tomato is a fruit, but Wisdom is knowing not to put it in a fruit salad."icon_razz.gif


Viewing all articles
Browse latest Browse all 8500

Trending Articles