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'
这是我想要使用的查询,如果存在于分类帐表的左外连接
中答案 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'
如果您共享原始逻辑
,请确保此代码可以简化