如何在join语句中使用IF EXISTS

时间:2016-01-29 06:06:11

标签: sql sql-server sql-server-2008

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 >= '2015-04-01' 
    AND LED.VOUCHERDATE <= '2015-05-31'

这是我想要使用的查询,如果存在于分类帐表的左外连接

1 个答案:

答案 0 :(得分:0)

Exists移至Where子句

SELECT *
FROM   LEDGER AS LED
       LEFT OUTER JOIN (SELECT COMPANYCODE,
                               Sum(AMOUNT * 0) AS AMOUNT
                        FROM   B_C_J_TRANS AS BCJTRANS
                        WHERE  ( COMPANYCODE = 'RAM' )
                               AND ( MODE = 'J' )
                               AND EXISTS(SELECT ACCOUNTCODE
                                          FROM   B_C_J_TRANS
                                          WHERE  ACCOUNTCODE = '001809'
                                                 AND COMPANYCODE = 'RAM' --AND ENTRYNO='25' 
                                                 AND MODE = 'J')
                        GROUP  BY COMPANYCODE
                        UNION
                        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' )
                               AND NOT EXISTS(SELECT ACCOUNTCODE
                                              FROM   B_C_J_TRANS
                                              WHERE  ACCOUNTCODE = '001809'
                                                     AND COMPANYCODE = 'RAM' --AND ENTRYNO='25' 
                                                     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 >= '2015-04-01'
       AND LED.VOUCHERDATE <= '2015-05-31' 

如果您共享原始逻辑

,请确保此代码可以简化