【问题标题】:MS Access error :the number of columns in the two selected tables does not matchMS Access 错误:两个选定表中的列数不匹配
【发布时间】:2012-12-04 03:50:17
【问题描述】:

我在 MS Access 查询中收到此错误:The number of columns in the two selected tables or queries of a union query do not match.

列数明确匹配,当我运行 UNION 查询一次选择任意 2 个集合时,它工作正常。当我在查询中包含超过 2 个选择时,它会显示此错误。

SELECT  "Applied" as Application_Status, Count(*) AS [CountOfApplication Status]
FROM [EDB Applicants - ALL]
WHERE [EDB Applicants - ALL].[Application Year].Value Like "2012" And [EDB Applicants - ALL].[Application Date]<=Date()-366 And [EDB Applicants - ALL].[Dec Date]>Date()-366

UNION

SELECT  "Hold" as Application_Status, Count(*) AS [CountOfApplication Status]
FROM [EDB Applicants - ALL]
WHERE [EDB Applicants - ALL].[Application Year].Value Like "2012" And [EDB Applicants - ALL].[Application Date]<=Date()-366 And [EDB Applicants - ALL].[Dec Date]<=Date()-366 And [EDB Applicants - ALL].[Application Status]="Hold"

UNION

SELECT  "Withdraw" as Application_Status, Count(*) AS [CountOfApplication Status]
FROM [EDB Applicants - ALL]
WHERE [EDB Applicants - ALL].[Application Year].Value Like "2012" And [EDB Applicants - ALL].[Application Date]<=Date()-366 And [EDB Applicants - ALL].[Date of Enrollment Change]<=Date()-366 And [EDB Applicants - ALL].[Application Status] In ("Declined","Withdrew Application","Withdrew After Enrollment")

【问题讨论】:

  • 您使用的是多值字段还是表单[edb applicants - all].[application year].value LIKE "2012"
  • 是的。我正在使用多值字段

标签: ms-access ms-access-2007 union


【解决方案1】:

我不知道你原来的问题的原因,但你可以重写你的查询来消除联合:

select Application_Status, COUNT(*)
from (select (case when  [EDB Applicants - ALL].[Application Year].Value Like "2012" And [EDB Applicants - ALL].[Application Date]<=Date()-366 And [EDB Applicants - ALL].[Dec Date]>Date()-366
                   then 'Applied'
                   when [EDB Applicants - ALL].[Application Year].Value Like "2012" And [EDB Applicants - ALL].[Application Date]<=Date()-366 And [EDB Applicants - ALL].[Dec Date]<=Date()-366 And [EDB Applicants - ALL].[Application Status]="Hold"
                   then 'Hold'
                   when [EDB Applicants - ALL].[Application Year].Value Like "2012" And [EDB Applicants - ALL].[Application Date]<=Date()-366 And [EDB Applicants - ALL].[Date of Enrollment Change]<=Date()-366 And [EDB Applicants - ALL].[Application Status] In ("Declined","Withdrew Application","Withdrew After Enrollment")
                   then 'Withdraw'
              end) as Application_Status, [EDB Applicants - ALL].*
      from [EDB Applicants - ALL]
     ) t
where Application_Status is not null
group by Application_Status

没错,在 MS Access 中你必须使用 IIF(),但同样的想法也适用:

select Application_Status, COUNT(*)
from (select iif([EDB Applicants - ALL].[Application Year].Value Like "2012" And [EDB Applicants - ALL].[Application Date]<=Date()-366 And [EDB Applicants - ALL].[Dec Date]>Date()-366,
                  'Applied',
                  iif([EDB Applicants - ALL].[Application Year].Value Like "2012" And [EDB Applicants - ALL].[Application Date]<=Date()-366 And [EDB Applicants - ALL].[Dec Date]<=Date()-366 And [EDB Applicants - ALL].[Application Status]="Hold",
                       'Hold',
                       iif([EDB Applicants - ALL].[Application Year].Value Like "2012" And [EDB Applicants - ALL].[Application Date]<=Date()-366 And [EDB Applicants - ALL].[Date of Enrollment Change]<=Date()-366 And [EDB Applicants - ALL].[Application Status] In ("Declined","Withdrew Application","Withdrew After Enrollment")
                        'Withdraw', '')))) as Application_Status, [EDB Applicants - ALL].*
      from [EDB Applicants - ALL]
     ) t
where Application_Status <> ''
group by Application_Status

【讨论】:

  • Case 不能作为 Access SQL 的一部分使用,您将需要 IIfSwitch 函数。
猜你喜欢
  • 1970-01-01
  • 2012-05-07
  • 2016-06-16
  • 1970-01-01
  • 2016-12-19
  • 2020-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多