Hi All,

I'm not sure what stupid mistake I'm making in the following SQL

The output results I think is satisfactory but the value in the"control fields “Readmission_Status” should be 0 and 1 respectively with following sequence output.


Paitiend_BK Admission_DTTM ReAdmisison_Status
83340 2013-03-16 18:55:00.0 1
83340 2013-04-28 19:28:00.0 1

Code:
SELECT d1.patient_bk,d1.admission_dttm,
       CASE 
          WHEN EXISTS (SELECT * FROM pp_rds.inpatient_spell d2 
                       WHERE  date(d2.admission_dttm) BETWEEN '2013-04-01' and '2013-04-28'
                       AND d2.Admission_method_NC like  '2%' 
                       AND d2.patient_bk = d1.patient_bk 
                       AND d2.patient_bk = 83340)
             THEN '1'
          --WHEN EXISTS (SELECT * FROM tab3 WHERE tab3.colA = tab3.colA) 
          --   THEN '2'
          ELSE '0'
       END AS ReAdmission_Status
  FROM pp_rds.inpatient_spell d1
  WHERE d1.patient_bk = 83340
  AND date(d1.admission_dttm) BETWEEN '2012-11-01' and '2013-04-28'
  AND   d1.Admission_method_NC like  '2%'
When I run the simply query its works as shown below:
Code:
select episode_id,d1,
   CASE 
    WHEN EXISTS
   	(Select * from emp d
   	where d.episode_id = a.episode_id
   	and d1 = 5)
   Then '1'
   Else '0'
   END AS colC
from emp a
Result OK

HTML Code:
EpisodeID   D1   colC
        845    51   0
          1  1575   0
          2     5   1
          3     5   1
          4  1154   0

I appreciate you help