SQL语法错误(从事务代码为211& 292的account_ID中排除事务代码为293的account_ID)
SELECT *
FROM
(SELECT ACCOUNT_ID
FROM Transaction_Data_Flexi
WHERE TRAN_CODE = 211 OR TRAN_CODE = 292) AS trn1
LEFT JOIN
(SELECT ACCOUNT_ID
FROM Transaction_Data_Flexi
WHERE TRAN_CODE = 293) AS trn2
WHERE
trn2.ACCOUNT_ID NOT IN trn1.ACCOUNT_ID
GROUP BY
ACCOUNT_ID
答案 0 :(得分:1)
您错过了加入条件ON
。如果您不打算指定条件,请使用CROSS JOIN
代替LEFT JOIN
。
还会有另一个错误 - 对于GROUP BY
子句,您需要指定要分组的列。
GROUP BY trn1.account_id, trn2.account_id
答案 1 :(得分:0)
我建议您使用group by
和having
:
SELECT ACCOUNT_ID
FROM Transaction_Data_Flexi
GROUP BY ACCOUNT_ID
HAVING SUM(IIF(TRAN_CODE = 211, 1, 0)) > 0 AND
SUM(IIF(TRAN_CODE = 292, 1, 0)) > 0 AND
SUM(IIF(TRAN_CODE = 293, 1, 0)) = 0;
这将检查给定事务的代码。 having
条件验证代码。
答案 2 :(得分:0)
谢谢大家的帮助!是的你应该工作,但我想尝试使用" NOT IN"声明,我已经更新了查询,如下所示。我仍然有问题在"""附近?中间的陈述:
SELECT trn1.ACCOUNT_ID
FROM
(
SELECT ACCOUNT_ID
FROM Transaction_Data_Flexi
WHERE TRAN_CODE = 211 OR TRAN_CODE = 292
) AS trn1
WHERE ACCOUNT_ID NOT IN
(
SELECT ACCOUNT_ID
FROM Transaction_Data_Flexi
WHERE TRAN_CODE = 293
) AS trn2
GROUP BY ACCOUNT_ID