错误的SQL查询

时间:2018-08-14 18:13:14

标签: sql ms-access

我在MS Access 2016中有4个表。它们是:

tblA:

Id Date        AmountA
1  18/8/18      10
1  18/8/18      11
2  19/8/18      10
2  19/8/18      12

tblB:

Id    AmountB
1      15
2      17

tblC:

Id     AmountC
1      2
2      3

我想要这个结果:

Id Date Total(AmountA) Total(AmountB) Total(AmountC)
1  18/8/18 21           15                 2
2  19/8/18 22           17                 3

1 个答案:

答案 0 :(得分:0)

尝试:

Select Id, max(Date) as Date, max(AmountA) as AmountA, max(AmountB) as AmountB, max(AmountC) as AmountC
FROM
(
SELECT Id, Date, sum(AmountA) as AmountA, Null as AmountB, Null as AmountC FROM tblA 
GROUP BY Id,Date
UNION ALL
SELECT Id, Null as Date, Null as Amount, sum(AmountB) as AmountB, Null as AmountC From tblB
Group by Id
UNION ALL
Select Id, Null as Date, Null as Amount, Null as AmountB, sum(AmountC) as AmountC From tblC
Group by Id
) as aggregate
Group by Id

此查询可能有几个错误,但一般结构应正确。我是几天前巧合的。