title: How to use IF EXISTS in join statement
body: <pre><code>SELECT 
      *
  FROM 
      LEDGER AS LED 
  LEFT OUTER JOIN
      (IF EXISTS(SELECT ACCOUNTCODE FROM B_C_J_TRANS WHERE ACCOUNTCODE = '001809' AND COMPANYCODE='RAM' --AND ENTRYNO='25' 
      AND MODE='J') 
          SELECT COMPANYCODE, SUM(AMOUNT * 0) AS AMOUNT
          FROM B_C_J_TRANS AS BCJTRANS
          WHERE (COMPANYCODE = 'RAM') AND (MODE = 'J') 
          GROUP BY COMPANYCODE
      ELSE
          SELECT COMPANYCODE, SUM( CASE WHEN BCJTRANS.ACCOUNTCODE = '800100' THEN BCJTRANS.AMOUNT ELSE 0 END) AS AMOUNT
          FROM B_C_J_TRANS AS BCJTRANS
          WHERE (COMPANYCODE = 'RAM')  AND (MODE = 'J')
          GROUP BY COMPANYCODE) AS A ON LED.COMPANYCODE = A.COMPANYCODE 
                                     AND LED.BOOKCODE = A.BOOKCODE 
                                     AND LED.[TYPE] = A.MODE 
                                     AND LED.FINANCIALYEAR = A.ENTRYYEAR 
                                     AND LED.VOUCHERNO = A.ENTRYNO 
  WHERE 
      LED.COMPANYCODE = 'RAM' 
      AND LED.ACCCODE = '800100' 
      AND LED.VOUCHERDATE &gt;= '2015-04-01' 
      AND LED.VOUCHERDATE &lt;= '2015-05-31'
  </code></pre>
  
  <p>This is my query i want to use if exist in left outer join of ledger table</p>