sql---example of the using of "case when"

/*tested in oracle*/
/* you can use select statment in then clause*/
SELECT CASE bh.zajlx
          WHEN '2'
             THEN (SELECT qt1.sfdb
                     FROM t03_qtajxx qt1
                    WHERE qt1.ajbh = bh.ajbh)
          WHEN '1'
             THEN (SELECT cta1.sfdb
                     FROM t03_ctais_ajxx cta1
                    WHERE cta1.ajbh = bh.ajbh)
          WHEN '3'
             THEN (SELECT cta1.sfdb
                     FROM t03_ctais_ajxx cta1
                    WHERE cta1.ajbh = bh.ajbh)
          ELSE 'X'
       END
  FROM t03_ajbh bh
WHERE bh.ajbh = '00200700003'

/*you can use in keyword in when clause*/
select t6.nsrdzdah, t6.ajszjg_j_dm, t6.jczt_dm,
        case
            when t6.jczt_dm  in (01,02,10,  20,21,22,30,  40,41,42,43,44,45,50,  60,70)
                then 'Y'
            else 'N'
         end aa
    from JC_AJXX t6 order by t6.nsrdzdah

你可能感兴趣的:(oracle,sql,J#)