5

MS Access クエリで次のエラーが発生しました。The number of columns in the two selected tables or queries of a union query do not match.

列の数は明らかに一致しており、一度に 2 つのセットを選択して UNION クエリを実行すると、正常に動作します。クエリに 2 つ以上の select を含めると、このエラーが表示されます。

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")
4

1 に答える 1

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
于 2012-12-03T19:11:24.937 に答える