[英]Summing two tables into one table in Jet SQL
Another SQL question if I may. 如果可以的话,另一个SQL问题。
I have two tables (generated from TRANSFORM - PIVOT queries in Jet SQL) 我有两个表(从TRANSFORM生成-Jet SQL中的PIVOT查询)
Category ID Account Jan Feb ... Dec
1 1 Cash 10 20 30
1 2 Card 100 200 300
1 3 Savings 200 400 600
and 和
Category ID Account Jan Feb ... Dec
1 1 Cash -5 -10 -20
1 2 Card -100 -200
1 3 Savings -100 -400
Category, ID and Account will always be the same in two tables. 在两个表中,类别,ID和帐户将始终相同。 There will be no accounts that occur in one table that don't occur in others. 一个表中不会出现其他表中不会出现的帐户。 There may be NULL values in either table but there will always be a matching cell in each table. 每个表中都可能有NULL值,但每个表中始终会有一个匹配的单元格。
What I would like is 我想要的是
Category ID Account Jan Feb ... Dec
1 1 Cash 5 10 10
1 2 Card 100 100 100
1 3 Savings 100 400 200
I have played around with UNION and JOIN queries but can't get there. 我玩过UNION和JOIN查询,但无法到达那里。
Thanks again, Andy 再次感谢,安迪
Edit - The original queries are 编辑-原始查询是
TRANSFORM Sum(Items.amount) AS total
SELECT Accounts.accCategory, Accounts.ID, Accounts.comment AS Account
FROM Accounts INNER JOIN Items ON Accounts.ID = Items.accFrom
WHERE (((Year([idate]))=2013) AND ((Items.category)<>3 Or (Items.category) Is Null) AND ((Accounts.accCategory)=6 OR (Accounts.accCategory)=7) AND ((Accounts.curr)=1))
GROUP BY Accounts.accCategory, Accounts.ID, Accounts.comment
PIVOT Format(idate,'mmm') IN ('Jan','Feb','Mar','Apr', 'May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
and 和
TRANSFORM Sum(Items.amount) AS total
SELECT Accounts.accCategory, Accounts.ID, Accounts.comment AS Account
FROM Accounts INNER JOIN Items ON Accounts.ID = Items.accFrom
WHERE (((Year([idate]))=2013) AND ((Items.category)=3) AND ((Items.comment)='Monthly') AND ((Accounts.accCategory)=6) AND ((Accounts.curr)=1))
GROUP BY Accounts.accCategory, Accounts.ID, Accounts.comment
PIVOT Format(idate,'mmm') In ('Jan','Feb','Mar','Apr', 'May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
One approach using the join of the 2 tables deb
and cred
is as follows: 使用两个表deb
和cred
一种方法如下:
SELECT cred.accCategory, cred.ID, cred.Account, [deb].[Jan]+[cred].[Jan] AS tot_Jan
FROM cred INNER JOIN deb ON (cred.Account = deb.Account) AND (cred.ID = deb.ID) AND (cred.accCategory = deb.accCategory);
This screenshot shows how the query is constructed: 此屏幕快照显示了查询的构造方式:
And here are the results (excuse the table formatting): 这是结果(请使用表格格式):
accCategory ID Account tot_Jan
1 1 Cash 5
1 2 Card 100
1 3 Savings 100
Be warned that the JOIN
is an equi-JOIN so it means the same number of records must exist in BOTH tables else you will find that credits where a debit does not exist will be dropped! 请注意, JOIN
是等分JOIN,因此这意味着两个表中必须存在相同数量的记录,否则您将发现不存在借方的贷方将被删除! So make sure that both input tables are consistent with the same key values (the 3 GROUP BY
fields in your earlier PIVOT query). 因此,请确保两个输入表都与相同的键值一致(先前的PIVOT查询中的3 GROUP BY
字段)。
The alternative approach is the UNION
with a GROUP BY
to calculate the total from it. 另一种方法是使用带有GROUP BY
的UNION
来计算总数。 I will paste that shortly. 我会尽快粘贴。
========== EDIT ============ ==========编辑============
You can build this up in stages. 您可以分阶段进行构建。 First I did SELECT * FROM cred UNION SELECT * FROM deb
and looked at in Design View. 首先,我执行了SELECT * FROM cred UNION SELECT * FROM deb
并在Design View中进行了研究。
Went back into the SQL view and made sure the UNION
query has brackets around it and SELECT sub.* FROM
before the brackets and AS SUB
after the brackets. 返回到SQL视图,并确保UNION
查询周围UNION
括号,并在方括号前有SELECT sub.* FROM
,在方括号后有AS SUB
。
Then I modified the query to include the GROUP BY
and Sum(Jan)
bits. 然后,我修改了查询以包括GROUP BY
和Sum(Jan)
位。 The final SQL is here and a screenshot follows. 最终的SQL在这里,然后是屏幕截图。
SELECT sub.accCategory, sub.ID, sub.Account, Sum(sub.Jan) AS SumOfJan
FROM (SELECT * FROM cred UNION SELECT * FROM deb) AS sub
GROUP BY sub.accCategory, sub.ID, sub.Account;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.