0

我知道关于此访问错误消息有几个问题,但它们不相关,因为我的查询不使用 DISTINCT 关键字。

我有两个类似的查询,一个包含贷方,另一个包含借方。它们按月份和类别分组。

最终,我希望在这两个表上进行完整的外部联接,以便我可以减去它们以获得每个类别中每个月的结果余额。

但是 Access 不允许完全外连接,所以我需要做一个 Right OUTER UNION LEFT OUTER WHERE Null。

我现在正在尝试在 Month 和 Category ID 字段上进行 RIGHT OUTER 连接。当我在一个字段上进行外部连接时,它按预期工作。当我在另一个字段上执行此操作时,它按预期工作,但是当我加入两个字段时,我得到“该字段太小,无法接受您尝试添加的数据量”

表格1:

积分查询

由。。。生产:

SELECT [transactions by category].[categoryid]                    AS CategoryID,
       Format([account transactions].[transaction date], "mm/yy") AS MonthYear,
       Nz(SUM([transactions by category].[amount]), 0)            AS
       [Category Total]
FROM   [transactions by category]
       INNER JOIN [account transactions]
               ON [account transactions].[id] =
                  [transactions by category].[transactionid]
WHERE  [account transactions].[transaction type] <> 8
GROUP  BY Format([account transactions].[transaction date], "mm/yy"),
          [transactions by category].[categoryid]; 

表 2:

借记表

由。。。生产:

SELECT [transactions by category].[categoryid],
       Format([account transactions].[transaction date], "mm/yy") AS MonthYear,
       Nz(SUM([transactions by category].[amount]), 0)            AS
       [Category Total]
FROM   [transactions by category]
       INNER JOIN [account transactions]
               ON [account transactions].[id] =
                  [transactions by category].[transactionid]
WHERE  [account transactions].[transaction type] = 8
GROUP  BY Format([account transactions].[transaction date], "mm/yy"),
          [transactions by category].[categoryid]; 

给我错误的右连接:

SELECT * FROM
  ((SELECT [transactions by category].[categoryid],
           Format([account transactions].[transaction date], "mm/yy") 
              AS MonthYear,
           Nz(SUM([transactions by category].[amount]), 0) AS [Category Total]
    FROM   [transactions by category]
           INNER JOIN [account transactions]
                   ON [account transactions].[id] =
                      [transactions by category].[transactionid]
    WHERE  [account transactions].[transaction type] = 8
    GROUP  BY Format([account transactions].[transaction date], "mm/yy"),
              [transactions by category].[categoryid]) AS [Category Returns]
RIGHT JOIN 
   (SELECT [transactions by category].[categoryid] AS CategoryID,
           Format([account transactions].[transaction date], "mm/yy") 
               AS MonthYear,
           Nz(SUM([transactions by category].[amount]), 0) AS [Category Total]
    FROM   [transactions by category]
           INNER JOIN [account transactions]
                   ON [account transactions].[id] = 
                      [transactions by category].[transactionid]
    WHERE  [account transactions].[transaction type] <> 8
    GROUP  BY Format([account transactions].[transaction date], "mm/yy"),
              [transactions by category].[categoryid]) AS [Category Debits]

ON [Category Returns].[categoryid] = [Category Debits].[categoryid]
   AND [Category Returns].[monthyear] = [Category Debits].[monthyear] ); 

似乎此错误发生在文本字段中。当我使用格式时,MonthYear 字段是否会变为文本字段?即便如此,它也只有 5 个字符长。另外,当我仅在 MonthYear 列上加入时,连接有效,但只有在我同时加入两个字段时才会失败。

4

1 回答 1

2

根据您的标签,您正在使用 SQL 服务器作为后端。您遇到的问题不是来自 MS Access,而是来自 MS Access 和 SQL Server 的组合。该查询将在纯 MS Access 环境中工作。(是的,格式确实转换为文本。)

您可以使用传递查询来创建使用 SQL Server 语法运行的查询,因此您可以使用完全外连接,或者您可能希望考虑http://weblogs.sqlteam.com/jeffs/archive/2007/04 /19/全外连接.aspx

编辑重新更改标签

如果您的某个字段是备注字段,请参阅http://support.microsoft.com/kb/896950。在像这样的复杂查询中,最好将备注字段修剪到 255 或更少。

于 2012-07-10T10:30:38.030 回答