title: Query for logistic regression, multiple where exists
body: <p>A logistic regression is a composed of a uniquely identifying number, followed by multiple binary variables (always 1 or 0) based on whether or not a person meets certain criteria.  Below I have a query that lists several of these binary conditions.  With only four such criteria the query takes a little longer to run than what I would think.  Is there a more efficient approach than below?  Note.  tblicd is a large table lookup table with text representations of 15k+ rows.  The query makes no real sense, just a proof of concept.  I have the proper indexes on my composite keys.</p>
  
  <pre><code>select  patient.patientid 
  ,case when exists
  (
      select c.patientid from tblclaims as c
      inner join patient as p on p.patientid=c.patientid
      and c.admissiondate = p.admissiondate
      and c.dischargedate = p.dischargedate
      where patient.patientid = p.patientid
      group by c.patientid
      having count(*) &gt; 1000
      )
      then '1' else '0'
      end as moreThan1000
  ,case when exists
  (
      select c.patientid from tblclaims as c
      inner join patient as p on p.patientid=c.patientid
      and c.admissiondate = p.admissiondate
      and c.dischargedate = p.dischargedate
      where patient.patientid = p.patientid
      group by c.patientid
      having count(*) &gt; 1500
      )
      then '1' else '0'
      end as moreThan1500
  ,case when exists
  (
      select distinct picd.patientid from patienticd as picd
      inner join patient as p on p.patientid= picd.patientid
      and picd.admissiondate = p.admissiondate
      and picd.dischargedate = p.dischargedate
      inner join tblicd as t on t.icd_id = picd.icd_id
      where t.descrip like '%diabetes%' and patient.patientid = picd.patientid
      )
      then '1' else '0'
      end as diabetes
  ,case when exists
  (
      select r.patientid, count(*) from patient as r
      where r.patientid = patient.patientid
      group by r.patientid
      having count(*) &gt;1
      ) 
      then '1' else '0'
      end 
  
  
  from patient
  order by moreThan1000 desc
  </code></pre>