我正在关注我在这里提出的问题:SumIF in SQL Access
我有3张桌子[经纪人],[OPT],[TRS]。 [经纪人]有2个字段BRKR_CODE, Status
和[OPT]和[TRS]有5个相同字段BRKR1, BRKR2, Date, COM_BRK1, COM_BRKR2
OPT.BRKR1
,OPT.BRKR2
,TRS.BRKR1
,TRS.BRKR2
与Broker.BRKR_CODE
相关联且可以相等。
我想写这个查询....如果Broker.Status="Active"
那么选择Broker.BRKR_CODE
然后对于每个Broker.BRKR_CODE
(例如“CB”),当OPT.COM_BRKR1
时OPT.BRKR1="CB"
+ OPT.COM_BRKR2
的总和OPT.BRKR2="CB"
时,TRS.COM_BRKR1
的总和在TRS.BRKR1="CB"
TRS.COM_BRKR2
+ TRS.BRKR2="CB"
的总和时BRKR_CODE
我使用前一个问题中得到的答案编写了这段代码...但仍然存在语法问题...基本上我想要一个包含3列OPT_Tot
,TRS_Tot
,{的数组{1}}
SELECT Broker.BRKR_CODE,
Sum(OPT.COM_BRKR1)+ Sum(OPT.COM_BRKR2) AS OPT_Tot,
Sum(TRS.COM_BRKR1)+ Sum(TRS.COM_BRKR2) AS TRS_Tot
FROM Broker
INNER JOIN OPT
ON (Broker.BRKR_CODE = OPT.BRKR2) OR (Broker.BRKR_CODE = OPT.BRKR1)
INNER JOIN TRS
ON (Broker.BRKR_CODE = TRS.BRKR2) OR (Broker.BRKR_CODE = TRS.BRKR1)
WHERE Broker.Status = "Active"
GROUP BY Broker.BRKR_CODE
答案 0 :(得分:1)
您应该尝试在设计器中进行此查询(只是为了使基本连接正确)。您将看到Access使用其他SQL引擎不做的括号做了一些不同的事情。
您需要将每组连接包装在括号中
SELECT Broker.BRKR_CODE,
Sum(OPT.COM_BRKR1)+ Sum(OPT.COM_BRKR2) AS OPT_Tot,
Sum(TRS.COM_BRKR1)+ Sum(TRS.COM_BRKR2) AS TRS_Tot
FROM (Broker
INNER JOIN OPT
ON (Broker.BRKR_CODE = OPT.BRKR2) OR (Broker.BRKR_CODE = OPT.BRKR1))
INNER JOIN TRS
ON (Broker.BRKR_CODE = TRS.BRKR2) OR (Broker.BRKR_CODE = TRS.BRKR1)
WHERE Broker.Status = "Active"
GROUP BY Broker.BRKR_CODE