多个UNION SQL Jet查询中的数据不匹配错误

时间:2014-07-09 22:16:09

标签: sql union jet

我在Jet SQL中有以下查询。基本上每个子查询A,B和D在最终表中生成一列。如果我按原样运行查询,则会在条件表达式中出现“数据类型不匹配”。如果我删除A,B,D中的任何一个它工作正常。我试过改变A / B / D的顺序没有效果。

如果我自己拿走每个A / B / D,他们就会执行罚款。

由于 安迪

SELECT A.accCategory, A.ID, A.Account, Sum(A.Total) AS Spent, Null As Allocated, Null As CurrentAllocated 
FROM (
SELECT Accounts.accCategory, Accounts.ID, Accounts.comment AS Account, Sum(IIf(Items.idate>=#07/01/2014 00:00:00# AND Items.idate<=#07/09/2014 00:00:00#,Items.amount,Null)) AS Total FROM Accounts INNER JOIN Items ON Accounts.ID = Items.accFrom WHERE (((Items.category)<>3 Or (Items.category) Is Null) AND ((Accounts.accCategory)=6) AND ((Accounts.curr)=1) AND ((Accounts.deleted)=False) AND ((Accounts.extra)<>'B')) GROUP BY Accounts.accCategory, Accounts.ID, Accounts.comment 
UNION SELECT Accounts.accCategory, Accounts.ID, Accounts.comment AS Account, Null FROM Accounts INNER JOIN Items ON Accounts.ID = Items.accFrom WHERE (((Accounts.accCategory)=6) AND ((Accounts.curr)=1) AND ((Accounts.deleted)=False) AND ((Accounts.extra)<>'B') AND Items.idate>=#07/01/2014 00:00:00# AND Items.idate<=#07/09/2014 00:00:00#) 
GROUP BY Accounts.accCategory, Accounts.ID, Accounts.comment) 
AS A 
GROUP BY A.accCategory, A.ID, A.Account 
UNION 
SELECT B.accCategory, B.ID, B.Account, Null as Spent, Sum(B.Total) AS Allocated, Null As CurrentAllocated 
FROM (
SELECT Accounts.accCategory, Accounts.ID, Accounts.comment AS Account, Sum(IIf(Items.idate>=#07/01/2014 00:00:00# AND Items.idate<=#07/09/2014 00:00:00#,Items.amount,Null)) AS Total FROM Accounts INNER JOIN Items ON Accounts.ID = Items.accFrom WHERE ((Items.comment)='Monthly') And ((Items.category) = 3) And ((Accounts.accCategory) = 6) And (Accounts.curr) = 1 And (Accounts.deleted)=False AND (Accounts.extra)<>'B' 
GROUP BY Accounts.accCategory, Accounts.ID, Accounts.comment 
UNION 
SELECT Accounts.accCategory, Accounts.ID, Accounts.comment AS Account, Null FROM Accounts INNER JOIN Items ON Accounts.ID = Items.accFrom WHERE (((Accounts.accCategory)=6) AND ((Accounts.curr)=1) AND ((Accounts.deleted)=False) AND ((Accounts.extra)<>'B') AND Items.idate>=#07/01/2014 00:00:00# AND Items.idate<=#07/09/2014 00:00:00#) GROUP BY Accounts.accCategory, Accounts.ID, Accounts.comment
) AS B 
GROUP BY B.accCategory, B.ID, B.Account 
UNION 
SELECT D.accCategory, D.ID, D.Account, Null as Spent, Null AS Allocated, Sum(D.Total) As CurrentAllocated 
FROM (
SELECT Accounts.accCategory, Accounts.ID, Accounts.comment AS Account, Sum(IIf(Items.idate>=#07/01/2014 00:00:00#,Items.amount,Null)) AS Total FROM Accounts INNER JOIN Items ON Accounts.ID = Items.accFrom WHERE ((Items.comment)='Monthly') And ((Items.category) = 3) And ((Accounts.accCategory) = 6) And (Accounts.curr) = 1 And (Accounts.deleted)=False AND (Accounts.extra)<>'B' 
GROUP BY Accounts.accCategory, Accounts.ID, Accounts.comment 
UNION 
SELECT Accounts.accCategory, Accounts.ID, Accounts.comment AS Account, Null FROM Accounts INNER JOIN Items ON Accounts.ID = Items.accFrom WHERE (((Accounts.accCategory)=6) AND ((Accounts.curr)=1) AND ((Accounts.deleted)=False) AND ((Accounts.extra)<>'B') AND Items.idate>=#07/01/2014 00:00:00#) GROUP BY Accounts.accCategory, Accounts.ID, Accounts.comment
) AS D 
GROUP BY D.accCategory, D.ID, D.Account;

1 个答案:

答案 0 :(得分:1)

感谢您指点我正确的方向。问题是Nulls。如果我添加以下内容则一切正常。

SELECT Z.accCategory, Z.ID, Z.Account, 0 as Spent, 0 AS Allocated, 0 As CurrentAllocated 
FROM (
SELECT Accounts.accCategory, Accounts.ID, Accounts.comment AS Account, 0 AS Total FROM Accounts INNER JOIN Items ON Accounts.ID = Items.accFrom WHERE Items.ID=0
) AS Z 
GROUP BY Z.accCategory, Z.ID, Z.Account
UNION
...

安迪