具有2个以上查询的Access 2007 SQL UNION会引发错误

时间:2013-10-03 14:54:39

标签: sql ms-access union

我有以下查询在Acces 2007上完美运行:

SELECT "Concept" AS Activity, ProjectRTMAcquisitionNeeds.Id AS id, ProjectRTMAcquisitionNeeds.AcquisitionNeed AS ItemLeft, (SELECT MAX(rev) FROM ProjectReferences AS PR WHERE PR.title = ProjectReferences.title) AS LeftRefLastRev, ProjectRTMSystemRequirements.SystemRequirement AS ItemRight, (SELECT MAX(rev) FROM ProjectReferences AS PR WHERE PR.title = ProjectReferences_1.title) AS RightRefLastRev, IIf([ProjectReferences].[rev]=[LeftRefLastRev] And [ProjectReferences_1].[rev]=[RightRefLastRev],1,0) AS Valid
FROM ((ProjectReferences AS ProjectReferences_1 INNER JOIN ProjectRTMSystemRequirements ON ProjectReferences_1.Id = ProjectRTMSystemRequirements.reference) INNER JOIN ProjectRTMAcquisitionNeeds ON ProjectRTMSystemRequirements.Id = ProjectRTMAcquisitionNeeds.SystemRequirement.Value) INNER JOIN ProjectReferences ON ProjectRTMAcquisitionNeeds.reference = ProjectReferences.Id

我有几个这样的查询,我想要“UNION”。如果我“UNION”只有2个查询它正在工作但是当我尝试“UNION更多那个2时,Access告诉我查询中的列数不相等。

我尝试“UNION”三次这样的查询(这样我确定三个查询中的列数相同):

SELECT "Concept" AS Activity, ProjectRTMAcquisitionNeeds.Id AS id, ProjectRTMAcquisitionNeeds.AcquisitionNeed AS ItemLeft, (SELECT MAX(rev) FROM ProjectReferences AS PR WHERE PR.title = ProjectReferences.title) AS LeftRefLastRev, ProjectRTMSystemRequirements.SystemRequirement AS ItemRight, (SELECT MAX(rev) FROM ProjectReferences AS PR WHERE PR.title = ProjectReferences_1.title) AS RightRefLastRev, IIf([ProjectReferences].[rev]=[LeftRefLastRev] And [ProjectReferences_1].[rev]=[RightRefLastRev],1,0) AS Valid
FROM ((ProjectReferences AS ProjectReferences_1 INNER JOIN ProjectRTMSystemRequirements ON ProjectReferences_1.Id = ProjectRTMSystemRequirements.reference) INNER JOIN ProjectRTMAcquisitionNeeds ON ProjectRTMSystemRequirements.Id = ProjectRTMAcquisitionNeeds.SystemRequirement.Value) INNER JOIN ProjectReferences ON ProjectRTMAcquisitionNeeds.reference = ProjectReferences.Id
UNION ALL
SELECT "Concept" AS Activity, ProjectRTMAcquisitionNeeds.Id AS id, ProjectRTMAcquisitionNeeds.AcquisitionNeed AS ItemLeft, (SELECT MAX(rev) FROM ProjectReferences AS PR WHERE PR.title = ProjectReferences.title) AS LeftRefLastRev, ProjectRTMSystemRequirements.SystemRequirement AS ItemRight, (SELECT MAX(rev) FROM ProjectReferences AS PR WHERE PR.title = ProjectReferences_1.title) AS RightRefLastRev, IIf([ProjectReferences].[rev]=[LeftRefLastRev] And [ProjectReferences_1].[rev]=[RightRefLastRev],1,0) AS Valid
FROM ((ProjectReferences AS ProjectReferences_1 INNER JOIN ProjectRTMSystemRequirements ON ProjectReferences_1.Id = ProjectRTMSystemRequirements.reference) INNER JOIN ProjectRTMAcquisitionNeeds ON ProjectRTMSystemRequirements.Id = ProjectRTMAcquisitionNeeds.SystemRequirement.Value) INNER JOIN ProjectReferences ON ProjectRTMAcquisitionNeeds.reference = ProjectReferences.Id
UNION ALL
SELECT "Concept" AS Activity, ProjectRTMAcquisitionNeeds.Id AS id, ProjectRTMAcquisitionNeeds.AcquisitionNeed AS ItemLeft, (SELECT MAX(rev) FROM ProjectReferences AS PR WHERE PR.title = ProjectReferences.title) AS LeftRefLastRev, ProjectRTMSystemRequirements.SystemRequirement AS ItemRight, (SELECT MAX(rev) FROM ProjectReferences AS PR WHERE PR.title = ProjectReferences_1.title) AS RightRefLastRev, IIf([ProjectReferences].[rev]=[LeftRefLastRev] And [ProjectReferences_1].[rev]=[RightRefLastRev],1,0) AS Valid
FROM ((ProjectReferences AS ProjectReferences_1 INNER JOIN ProjectRTMSystemRequirements ON ProjectReferences_1.Id = ProjectRTMSystemRequirements.reference) INNER JOIN ProjectRTMAcquisitionNeeds ON ProjectRTMSystemRequirements.Id = ProjectRTMAcquisitionNeeds.SystemRequirement.Value) INNER JOIN ProjectReferences ON ProjectRTMAcquisitionNeeds.reference = ProjectReferences.Id

但它也没有用。

你对我做错了什么有任何想法吗?

提前致谢。

1 个答案:

答案 0 :(得分:0)

SELECT A.Activity,A.id,A.ItemLeft,A.LeftRefLastRev,A.ItemRight,A.RightRefLastRev,A.Valid
FROM           
    (
        SELECT "Concept" AS Activity, ProjectRTMAcquisitionNeeds.Id AS id, ProjectRTMAcquisitionNeeds.AcquisitionNeed AS ItemLeft, (SELECT MAX(rev) FROM ProjectReferences AS PR WHERE PR.title = ProjectReferences.title) AS LeftRefLastRev, ProjectRTMSystemRequirements.SystemRequirement AS ItemRight, (SELECT MAX(rev) FROM ProjectReferences AS PR WHERE PR.title = ProjectReferences_1.title) AS RightRefLastRev, IIf([ProjectReferences].[rev]=[LeftRefLastRev] And [ProjectReferences_1].[rev]=[RightRefLastRev],1,0) AS Valid
        FROM ((ProjectReferences AS ProjectReferences_1 INNER JOIN ProjectRTMSystemRequirements ON ProjectReferences_1.Id = ProjectRTMSystemRequirements.reference) INNER JOIN ProjectRTMAcquisitionNeeds ON ProjectRTMSystemRequirements.Id = ProjectRTMAcquisitionNeeds.SystemRequirement.Value) INNER JOIN ProjectReferences ON ProjectRTMAcquisitionNeeds.reference = ProjectReferences.Id
        UNION ALL
        SELECT "Concept" AS Activity, ProjectRTMAcquisitionNeeds.Id AS id, ProjectRTMAcquisitionNeeds.AcquisitionNeed AS ItemLeft, (SELECT MAX(rev) FROM ProjectReferences AS PR WHERE PR.title = ProjectReferences.title) AS LeftRefLastRev, ProjectRTMSystemRequirements.SystemRequirement AS ItemRight, (SELECT MAX(rev) FROM ProjectReferences AS PR WHERE PR.title = ProjectReferences_1.title) AS RightRefLastRev, IIf([ProjectReferences].[rev]=[LeftRefLastRev] And [ProjectReferences_1].[rev]=[RightRefLastRev],1,0) AS Valid
        FROM ((ProjectReferences AS ProjectReferences_1 INNER JOIN ProjectRTMSystemRequirements ON ProjectReferences_1.Id = ProjectRTMSystemRequirements.reference) INNER JOIN ProjectRTMAcquisitionNeeds ON ProjectRTMSystemRequirements.Id = ProjectRTMAcquisitionNeeds.SystemRequirement.Value) INNER JOIN ProjectReferences ON ProjectRTMAcquisitionNeeds.reference = ProjectReferences.Id
    ) A
        UNION ALL
        SELECT "Concept" AS Activity, ProjectRTMAcquisitionNeeds.Id AS id, ProjectRTMAcquisitionNeeds.AcquisitionNeed AS ItemLeft, (SELECT MAX(rev) FROM ProjectReferences AS PR WHERE PR.title = ProjectReferences.title) AS LeftRefLastRev, ProjectRTMSystemRequirements.SystemRequirement AS ItemRight, (SELECT MAX(rev) FROM ProjectReferences AS PR WHERE PR.title = ProjectReferences_1.title) AS RightRefLastRev, IIf([ProjectReferences].[rev]=[LeftRefLastRev] And [ProjectReferences_1].[rev]=[RightRefLastRev],1,0) AS Valid