如果有人可以帮助我解决这个问题,我们将不胜感激。
我正在尝试从一个使用 sum() 函数并连接另外两个表的表中获取结果。我的结果在 sum 列上相乘。这是我的表格的样子和我想要的结果
表格1
[Id] [Account_nbr] [date] [seq#]
------------------------------------------
[1234] [$60] [4321] [10-15-2012] [1]
[1234] [$20] [4321] [10-15-2012] [2]
[1234] [$30] [4321] [10-15-2012] [3]
[2345] [$40] [9876] [10-15-2012] [1]
[3456] [$50] [6543] [10-15-2012] [1]
表2
[ID] [cust_num]
---------------------
[1234] [8765]
[2345] [8766]
[3456] [8767]
表3
[cust_num] [account_nbr]
-------------------------------
[8765] [4321]
[8767] [9876]
我想要的结果将是 join Table 1
、Table 2
usingID
和 join Table 3
usingcust_num
和 see account_nbr
fromtable 1
与account_number
in匹配Table 3
,如果找到匹配则
sum(Table1.Amount),Table1.Id,Table1.Account_nbr,Table1.Date
我正在使用这样的 SQL 查询,但我的总和结果成倍增加
SELECT
sum((Table1.Amount), Table1.Id, Table1.Account_nbr, Table1.Date
FROM
table1, table2, table3
WHERE
table1.id = table2.id
AND table2.cust_num = table3.cust_num
AND table1.account_nbr = table3.account_nbr
GROUP BY
table1.id,table1.account_nbr,table1.date
ORDER BY
table1.date DESC
但正如我之前所说,我的结果正在成倍增加。我想要的表格形式的结果如下所述
[Amount] [Id] [Account_nbr] [Date]
---------------------------------------------------
[$110] [1234] [4321] [10-15-2012]
[$40] [2345] [9876] [10-15-2012]
id = 3456
不应该在那里,因为相应的account_nbr
from 中table1
不存在table3
。