为什么 UNION 导致 ''multi part identifier could not be bound' 错误

Why is UNION causing ''multi part identifier could not be bound' error

关于这个问题,我已经阅读了许多关于 SO 的不同问题,但是我仍然无法找到我的代码有什么问题。我为此苦苦挣扎了好几天,现在想不出还有什么可以尝试的。 我可以 运行 SQL 查询窗格中的整个代码及其 运行s 并给出预期结果,但是当放入 SSRS 报告时显示 'multi part identifier....' 错误。如果我将 UNION 报告拆分为 2 个独立的部分,则每个部分都可以加载到 SSRS 报告中,而不会显示任何错误。 阅读了以下帖子后,我检查了: The multi-part identifier could not be bound --change Group By--(尝试使用和不使用 Order By)

The multi-part identifier could not be bound --隐式和显式连接--(我认为所有连接都是显式的)

The multi-part identifier could not be bound 检查 table 命名——(我认为所有 table 都使用别名重命名并正确连接)

我还检查了联合的两半中的所有命名是否相同,所以我很困惑为什么联合会导致错误。

我的代码: (抱歉篇幅太长,但很难知道如何简化它并给出问题的真实印象)

--The multi-part identifier "gor.gorec" could not be bound.
--The multi-part identifier "gos.gosent" could not be bound.
--C1 

--all ms 46 completed where same company not counted as output previously--
SELECT        m.ccx_name AS MilestoneName, m.ccx_status, m.ccx_statusname AS MilestoneStatus, proj.ccx_programmename AS Programme, 'C1' AS OutputNumber, proj.ccx_projectnumber AS ProjectNumber, 
                         comp.name AS CompanyName, CASE WHEN comp.tsg_localregionpicklistname IN ('Devon', 'Somerset') 
                         THEN 'Heart of South West' WHEN comp.tsg_localregionpicklistname LIKE 'Isles of Scilly' THEN 'Cornwall' WHEN comp.tsg_localregionpicklistname IN ('Gloucestershire', 'Cornwall', 'Swindon & Wiltshire', 
                         'West of England', 'Dorset') THEN comp.tsg_localregionpicklistname ELSE 'Out of Region' END AS LEPregion, proj.ccx_projectstart AS ProjectStartDate, m.ccx_datecompleted AS AchievedDate, 
                         jsc.fs_anticipatedjobs AS AnticipatedJobs, g.ccx_valueofgrant AS GrantOffer, gor.gorec as GrantOfferReceived, gos.gosent as GrantOfferSent,
                        CASE WHEN gor.gorec = 1 THEN 0
                        WHEN gos.gosent = 1 THEN 1
                        ELSE '' END AS AnticipatedGrant,
                         consult.fullname AS Adviser
FROM               Filteredccx_milestone AS m INNER JOIN
                         Filteredccx_project AS proj ON m.ccx_project = proj.ccx_projectid INNER JOIN
                         FilteredAccount AS comp ON proj.ccx_customerid = comp.accountid LEFT OUTER JOIN
                         Filteredccx_jobscreatedorsafeguarded AS jsc ON proj.ccx_projectid = jsc.ccx_project LEFT OUTER JOIN
                         Filteredccx_grant AS g ON proj.ccx_grantid = g.ccx_grantid LEFT OUTER JOIN
                         Filteredccx_ccx_project_contact_consultant AS link ON proj.ccx_projectid = link.ccx_projectid LEFT OUTER JOIN
                         FilteredContact AS consult ON link.contactid = consult.contactid LEFT OUTER JOIN
                         (select M1.ccx_project,1 as gorec
                                FROM Filteredccx_milestone as M1
                                WHERE (M1.ccx_name LIKE '45b%') AND (M1.ccx_status = '803080001'))AS gor ON gor.ccx_project=proj.ccx_projectid LEFT OUTER JOIN
                         (select M2.ccx_project,1 as gosent
                                FROM Filteredccx_milestone as M2
                                WHERE (M2.ccx_name LIKE '45 Grant%') AND (M2.ccx_status = '803080001')) AS gos ON gos.ccx_project=proj.ccx_projectid

WHERE        (m.ccx_name LIKE '46%') AND (m.ccx_datecompleted BETWEEN '2017/04/01' AND '2017/07/01') AND (m.ccx_status = '803080001') AND 
                         (proj.ccx_programme = '04D0B1DF-FD36-E611-8870-005056A52209') AND (comp.tsg_companyuid NOT IN ('COMP00153968', 'COMP00091748', 'COMP00091284', 'COMP00177586', 'COMP00231427', 
                         'COMP00077428', 'COMP00077490')) OR
                         (m.ccx_name LIKE '46%') AND (m.ccx_datecompleted BETWEEN '2017/04/01' AND '2017/07/01') AND (m.ccx_status = '803080001') AND 
                         (proj.ccx_programme = '14286237-FE36-E611-8870-005056A52209') AND (comp.tsg_companyuid NOT IN ('COMP00153968', 'COMP00091748', 'COMP00091284', 'COMP00177586', 'COMP00231427', 
                         'COMP00077428', 'COMP00077490')) AND (comp.accountid NOT IN
                             (SELECT        FilteredAccount.accountid
                               FROM            Filteredccx_milestone INNER JOIN
                                                         Filteredccx_project ON Filteredccx_milestone.ccx_project = Filteredccx_project.ccx_projectid INNER JOIN
                                                         FilteredAccount ON Filteredccx_project.ccx_customerid = FilteredAccount.accountid
                               WHERE        (Filteredccx_milestone.ccx_name LIKE '46%') AND (Filteredccx_milestone.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone.ccx_status = '803080001') AND 
                                                         (Filteredccx_project.ccx_programme = '04D0B1DF-FD36-E611-8870-005056A52209') OR
                                                         (Filteredccx_milestone.ccx_name LIKE '46%') AND (Filteredccx_milestone.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone.ccx_status = '803080001') AND 
                                                         (Filteredccx_project.ccx_programme = '14286237-FE36-E611-8870-005056A52209') OR
                                                         (Filteredccx_milestone.ccx_name LIKE '45b%') AND (Filteredccx_milestone.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone.ccx_status = '803080001') AND 
                                                         (Filteredccx_project.ccx_programme = 'D8943876-545A-E711-80FE-0050569FE3BD') OR
                                                         (Filteredccx_milestone.ccx_name LIKE '45b%') AND (Filteredccx_milestone.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone.ccx_status = '803080001') AND 
                                                         (Filteredccx_project.ccx_programme = '14286237-FE36-E611-8870-005056A52209') OR
                                                         (Filteredccx_milestone.ccx_name LIKE '45b%') AND (Filteredccx_milestone.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone.ccx_status = '803080001') AND 
                                                         (Filteredccx_project.ccx_programme = '04D0B1DF-FD36-E611-8870-005056A52209')))
UNION
SELECT        m.ccx_name AS MilestoneName, m.ccx_status, m.ccx_statusname AS MilestoneStatus, proj.ccx_programmename AS Programme, 'C1' AS OutputNumber, proj.ccx_projectnumber AS ProjectNumber, 
                         comp.name AS CompanyName, CASE WHEN comp.tsg_localregionpicklistname IN ('Devon', 'Somerset') 
                         THEN 'Heart of South West' WHEN comp.tsg_localregionpicklistname LIKE 'Isles of Scilly' THEN 'Cornwall' WHEN comp.tsg_localregionpicklistname IN ('Gloucestershire', 'Cornwall', 'Swindon & Wiltshire', 
                         'West of England', 'Dorset') THEN comp.tsg_localregionpicklistname ELSE 'Out of Region' END AS LEPregion, proj.ccx_projectstart AS ProjectStartDate, m.ccx_datecompleted AS AchievedDate, 
                         jsc.fs_anticipatedjobs AS AnticipatedJobs, g.ccx_valueofgrant AS GrantOffer, gor.gorec as GrantOfferReceived, gos.gosent as GrantOfferSent,
                        CASE WHEN gor.gorec = 1 THEN 0
                        WHEN gos.gosent = 1 THEN 1
                        ELSE '' END AS AnticipatedGrant, 
                          consult.fullname AS Adviser
FROM               Filteredccx_milestone AS m INNER JOIN
                         Filteredccx_project AS proj ON m.ccx_project = proj.ccx_projectid INNER JOIN
                         FilteredAccount AS comp ON proj.ccx_customerid = comp.accountid LEFT OUTER JOIN
                         Filteredccx_jobscreatedorsafeguarded AS jsc ON proj.ccx_projectid = jsc.ccx_project LEFT OUTER JOIN
                         Filteredccx_grant AS g ON proj.ccx_grantid = g.ccx_grantid LEFT OUTER JOIN
                         Filteredccx_ccx_project_contact_consultant AS link ON proj.ccx_projectid = link.ccx_projectid LEFT OUTER JOIN
                         FilteredContact AS consult ON link.contactid = consult.contactid LEFT OUTER JOIN

                        (select M1.ccx_project,1 as gorec
                                FROM Filteredccx_milestone as M1
                                WHERE (M1.ccx_name LIKE '45b%') AND (M1.ccx_status = '803080001'))AS gor ON gor.ccx_project=proj.ccx_projectid LEFT OUTER JOIN

                         (select M2.ccx_project,1 as gosent
                                FROM Filteredccx_milestone as M2
                                WHERE (M2.ccx_name LIKE '45 Grant%') AND (M2.ccx_status = '803080001')) AS gos ON gos.ccx_project=proj.ccx_projectid

WHERE        (m.ccx_name LIKE '45b%') AND (m.ccx_datecompleted BETWEEN '2017/04/01' AND '2017/07/01') AND (m.ccx_status = '803080001') AND 
                         (proj.ccx_programme = 'D8943876-545A-E711-80FE-0050569FE3BD') AND (comp.tsg_companyuid NOT IN ('COMP00153968', 'COMP00091748', 'COMP00091284', 'COMP00177586', 'COMP00231427', 
                         'COMP00077428', 'COMP00077490')) AND (comp.accountid NOT IN
                             (SELECT        FilteredAccount_3.accountid
                               FROM            Filteredccx_milestone AS Filteredccx_milestone_3 INNER JOIN
                                                         Filteredccx_project AS Filteredccx_project_3 ON Filteredccx_milestone_3.ccx_project = Filteredccx_project_3.ccx_projectid INNER JOIN
                                                         FilteredAccount AS FilteredAccount_3 ON Filteredccx_project_3.ccx_customerid = FilteredAccount_3.accountid
                               WHERE        (Filteredccx_milestone_3.ccx_name LIKE '46%') AND (Filteredccx_milestone_3.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone_3.ccx_status = '803080001') AND 
                                                         (Filteredccx_project_3.ccx_programme = '04D0B1DF-FD36-E611-8870-005056A52209') OR
                                                         (Filteredccx_milestone_3.ccx_name LIKE '46%') AND (Filteredccx_milestone_3.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone_3.ccx_status = '803080001') AND 
                                                         (Filteredccx_project_3.ccx_programme = '14286237-FE36-E611-8870-005056A52209') OR
                                                         (Filteredccx_milestone_3.ccx_name LIKE '45b%') AND (Filteredccx_milestone_3.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone_3.ccx_status = '803080001') AND 
                                                         (Filteredccx_project_3.ccx_programme = 'D8943876-545A-E711-80FE-0050569FE3BD') OR
                                                         (Filteredccx_milestone_3.ccx_name LIKE '45b%') AND (Filteredccx_milestone_3.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone_3.ccx_status = '803080001') AND 
                                                         (Filteredccx_project_3.ccx_programme = '14286237-FE36-E611-8870-005056A52209') OR
                                                         (Filteredccx_milestone_3.ccx_name LIKE '45b%') AND (Filteredccx_milestone_3.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone_3.ccx_status = '803080001') AND 
                                                         (Filteredccx_project_3.ccx_programme = '04D0B1DF-FD36-E611-8870-005056A52209'))) OR

                         (m.ccx_name LIKE '45b%') AND (m.ccx_datecompleted BETWEEN '2017/04/01' AND '2017/07/01') AND (m.ccx_status = '803080001') AND 
                         (proj.ccx_programme = '14286237-FE36-E611-8870-005056A52209') AND (comp.tsg_companyuid NOT IN ('COMP00153968', 'COMP00091748', 'COMP00091284', 'COMP00177586', 'COMP00231427', 
                         'COMP00077428', 'COMP00077490')) AND (comp.accountid NOT IN
                             (SELECT        FilteredAccount_2.accountid
                               FROM            Filteredccx_milestone AS Filteredccx_milestone_2 INNER JOIN
                                                         Filteredccx_project AS Filteredccx_project_2 ON Filteredccx_milestone_2.ccx_project = Filteredccx_project_2.ccx_projectid INNER JOIN
                                                         FilteredAccount AS FilteredAccount_2 ON Filteredccx_project_2.ccx_customerid = FilteredAccount_2.accountid
                               WHERE        (Filteredccx_milestone_2.ccx_name LIKE '46%') AND (Filteredccx_milestone_2.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone_2.ccx_status = '803080001') AND 
                                                         (Filteredccx_project_2.ccx_programme = '04D0B1DF-FD36-E611-8870-005056A52209') OR
                                                         (Filteredccx_milestone_2.ccx_name LIKE '46%') AND (Filteredccx_milestone_2.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone_2.ccx_status = '803080001') AND 
                                                         (Filteredccx_project_2.ccx_programme = '14286237-FE36-E611-8870-005056A52209') OR
                                                         (Filteredccx_milestone_2.ccx_name LIKE '45b%') AND (Filteredccx_milestone_2.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone_2.ccx_status = '803080001') AND 
                                                         (Filteredccx_project_2.ccx_programme = 'D8943876-545A-E711-80FE-0050569FE3BD') OR
                                                         (Filteredccx_milestone_2.ccx_name LIKE '45b%') AND (Filteredccx_milestone_2.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone_2.ccx_status = '803080001') AND 
                                                         (Filteredccx_project_2.ccx_programme = '14286237-FE36-E611-8870-005056A52209') OR
                                                         (Filteredccx_milestone_2.ccx_name LIKE '45b%') AND (Filteredccx_milestone_2.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone_2.ccx_status = '803080001') AND 
                                                         (Filteredccx_project_2.ccx_programme = '04D0B1DF-FD36-E611-8870-005056A52209'))) OR

                         (m.ccx_name LIKE '45b%') AND (m.ccx_datecompleted BETWEEN '2017/04/01' AND '2017/07/01') AND (m.ccx_status = '803080001') AND 
                         (proj.ccx_programme = '04D0B1DF-FD36-E611-8870-005056A52209') AND (comp.tsg_companyuid NOT IN ('COMP00153968', 'COMP00091748', 'COMP00091284', 'COMP00177586', 'COMP00231427', 
                         'COMP00077428', 'COMP00077490')) AND (comp.accountid NOT IN
                             (SELECT        FilteredAccount_1.accountid
                               FROM            Filteredccx_milestone AS Filteredccx_milestone_1 INNER JOIN
                                                         Filteredccx_project AS Filteredccx_project_1 ON Filteredccx_milestone_1.ccx_project = Filteredccx_project_1.ccx_projectid INNER JOIN
                                                         FilteredAccount AS FilteredAccount_1 ON Filteredccx_project_1.ccx_customerid = FilteredAccount_1.accountid
                               WHERE        (Filteredccx_milestone_1.ccx_name LIKE '46%') AND (Filteredccx_milestone_1.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone_1.ccx_status = '803080001') AND 
                                                         (Filteredccx_project_1.ccx_programme = '04D0B1DF-FD36-E611-8870-005056A52209') OR
                                                         (Filteredccx_milestone_1.ccx_name LIKE '46%') AND (Filteredccx_milestone_1.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone_1.ccx_status = '803080001') AND 
                                                         (Filteredccx_project_1.ccx_programme = '14286237-FE36-E611-8870-005056A52209') OR
                                                         (Filteredccx_milestone_1.ccx_name LIKE '45b%') AND (Filteredccx_milestone_1.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone_1.ccx_status = '803080001') AND 
                                                         (Filteredccx_project_1.ccx_programme = 'D8943876-545A-E711-80FE-0050569FE3BD') OR
                                                         (Filteredccx_milestone_1.ccx_name LIKE '45b%') AND (Filteredccx_milestone_1.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone_1.ccx_status = '803080001') AND 
                                                         (Filteredccx_project_1.ccx_programme = '14286237-FE36-E611-8870-005056A52209') OR
                                                         (Filteredccx_milestone_1.ccx_name LIKE '45b%') AND (Filteredccx_milestone_1.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone_1.ccx_status = '803080001') AND 
                                                         (Filteredccx_project_1.ccx_programme = '04D0B1DF-FD36-E611-8870-005056A52209')))
ORDER BY CompanyName

创建一个视图并让 SSRS 使用它;

create view YourData

as

SELECT        m.ccx_name AS MilestoneName, m.ccx_status, m.ccx_statusname AS MilestoneStatus, proj.ccx_programmename AS Programme, 'C1' AS OutputNumber, proj.ccx_projectnumber AS ProjectNumber, 
                         comp.name AS CompanyName, CASE WHEN comp.tsg_localregionpicklistname IN ('Devon', 'Somerset') 
                         THEN 'Heart of South West' WHEN comp.tsg_localregionpicklistname LIKE 'Isles of Scilly' THEN 'Cornwall' WHEN comp.tsg_localregionpicklistname IN ('Gloucestershire', 'Cornwall', 'Swindon & Wiltshire', 
                         'West of England', 'Dorset') THEN comp.tsg_localregionpicklistname ELSE 'Out of Region' END AS LEPregion, proj.ccx_projectstart AS ProjectStartDate, m.ccx_datecompleted AS AchievedDate, 
                         jsc.fs_anticipatedjobs AS AnticipatedJobs, g.ccx_valueofgrant AS GrantOffer, gor.gorec as GrantOfferReceived, gos.gosent as GrantOfferSent,
                        CASE WHEN gor.gorec = 1 THEN 0
                        WHEN gos.gosent = 1 THEN 1
                        ELSE '' END AS AnticipatedGrant,
                         consult.fullname AS Adviser
FROM               Filteredccx_milestone AS m INNER JOIN
                         Filteredccx_project AS proj ON m.ccx_project = proj.ccx_projectid INNER JOIN
                         FilteredAccount AS comp ON proj.ccx_customerid = comp.accountid LEFT OUTER JOIN
                         Filteredccx_jobscreatedorsafeguarded AS jsc ON proj.ccx_projectid = jsc.ccx_project LEFT OUTER JOIN
                         Filteredccx_grant AS g ON proj.ccx_grantid = g.ccx_grantid LEFT OUTER JOIN
                         Filteredccx_ccx_project_contact_consultant AS link ON proj.ccx_projectid = link.ccx_projectid LEFT OUTER JOIN
                         FilteredContact AS consult ON link.contactid = consult.contactid LEFT OUTER JOIN
                         (select M1.ccx_project,1 as gorec
                                FROM Filteredccx_milestone as M1
                                WHERE (M1.ccx_name LIKE '45b%') AND (M1.ccx_status = '803080001'))AS gor ON gor.ccx_project=proj.ccx_projectid LEFT OUTER JOIN
                         (select M2.ccx_project,1 as gosent
                                FROM Filteredccx_milestone as M2
                                WHERE (M2.ccx_name LIKE '45 Grant%') AND (M2.ccx_status = '803080001')) AS gos ON gos.ccx_project=proj.ccx_projectid

WHERE        (m.ccx_name LIKE '46%') AND (m.ccx_datecompleted BETWEEN '2017/04/01' AND '2017/07/01') AND (m.ccx_status = '803080001') AND 
                         (proj.ccx_programme = '04D0B1DF-FD36-E611-8870-005056A52209') AND (comp.tsg_companyuid NOT IN ('COMP00153968', 'COMP00091748', 'COMP00091284', 'COMP00177586', 'COMP00231427', 
                         'COMP00077428', 'COMP00077490')) OR
                         (m.ccx_name LIKE '46%') AND (m.ccx_datecompleted BETWEEN '2017/04/01' AND '2017/07/01') AND (m.ccx_status = '803080001') AND 
                         (proj.ccx_programme = '14286237-FE36-E611-8870-005056A52209') AND (comp.tsg_companyuid NOT IN ('COMP00153968', 'COMP00091748', 'COMP00091284', 'COMP00177586', 'COMP00231427', 
                         'COMP00077428', 'COMP00077490')) AND (comp.accountid NOT IN
                             (SELECT        FilteredAccount.accountid
                               FROM            Filteredccx_milestone INNER JOIN
                                                         Filteredccx_project ON Filteredccx_milestone.ccx_project = Filteredccx_project.ccx_projectid INNER JOIN
                                                         FilteredAccount ON Filteredccx_project.ccx_customerid = FilteredAccount.accountid
                               WHERE        (Filteredccx_milestone.ccx_name LIKE '46%') AND (Filteredccx_milestone.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone.ccx_status = '803080001') AND 
                                                         (Filteredccx_project.ccx_programme = '04D0B1DF-FD36-E611-8870-005056A52209') OR
                                                         (Filteredccx_milestone.ccx_name LIKE '46%') AND (Filteredccx_milestone.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone.ccx_status = '803080001') AND 
                                                         (Filteredccx_project.ccx_programme = '14286237-FE36-E611-8870-005056A52209') OR
                                                         (Filteredccx_milestone.ccx_name LIKE '45b%') AND (Filteredccx_milestone.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone.ccx_status = '803080001') AND 
                                                         (Filteredccx_project.ccx_programme = 'D8943876-545A-E711-80FE-0050569FE3BD') OR
                                                         (Filteredccx_milestone.ccx_name LIKE '45b%') AND (Filteredccx_milestone.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone.ccx_status = '803080001') AND 
                                                         (Filteredccx_project.ccx_programme = '14286237-FE36-E611-8870-005056A52209') OR
                                                         (Filteredccx_milestone.ccx_name LIKE '45b%') AND (Filteredccx_milestone.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone.ccx_status = '803080001') AND 
                                                         (Filteredccx_project.ccx_programme = '04D0B1DF-FD36-E611-8870-005056A52209')))
UNION
SELECT        m.ccx_name AS MilestoneName, m.ccx_status, m.ccx_statusname AS MilestoneStatus, proj.ccx_programmename AS Programme, 'C1' AS OutputNumber, proj.ccx_projectnumber AS ProjectNumber, 
                         comp.name AS CompanyName, CASE WHEN comp.tsg_localregionpicklistname IN ('Devon', 'Somerset') 
                         THEN 'Heart of South West' WHEN comp.tsg_localregionpicklistname LIKE 'Isles of Scilly' THEN 'Cornwall' WHEN comp.tsg_localregionpicklistname IN ('Gloucestershire', 'Cornwall', 'Swindon & Wiltshire', 
                         'West of England', 'Dorset') THEN comp.tsg_localregionpicklistname ELSE 'Out of Region' END AS LEPregion, proj.ccx_projectstart AS ProjectStartDate, m.ccx_datecompleted AS AchievedDate, 
                         jsc.fs_anticipatedjobs AS AnticipatedJobs, g.ccx_valueofgrant AS GrantOffer, gor.gorec as GrantOfferReceived, gos.gosent as GrantOfferSent,
                        CASE WHEN gor.gorec = 1 THEN 0
                        WHEN gos.gosent = 1 THEN 1
                        ELSE '' END AS AnticipatedGrant, 
                          consult.fullname AS Adviser
FROM               Filteredccx_milestone AS m INNER JOIN
                         Filteredccx_project AS proj ON m.ccx_project = proj.ccx_projectid INNER JOIN
                         FilteredAccount AS comp ON proj.ccx_customerid = comp.accountid LEFT OUTER JOIN
                         Filteredccx_jobscreatedorsafeguarded AS jsc ON proj.ccx_projectid = jsc.ccx_project LEFT OUTER JOIN
                         Filteredccx_grant AS g ON proj.ccx_grantid = g.ccx_grantid LEFT OUTER JOIN
                         Filteredccx_ccx_project_contact_consultant AS link ON proj.ccx_projectid = link.ccx_projectid LEFT OUTER JOIN
                         FilteredContact AS consult ON link.contactid = consult.contactid LEFT OUTER JOIN

                        (select M1.ccx_project,1 as gorec
                                FROM Filteredccx_milestone as M1
                                WHERE (M1.ccx_name LIKE '45b%') AND (M1.ccx_status = '803080001'))AS gor ON gor.ccx_project=proj.ccx_projectid LEFT OUTER JOIN

                         (select M2.ccx_project,1 as gosent
                                FROM Filteredccx_milestone as M2
                                WHERE (M2.ccx_name LIKE '45 Grant%') AND (M2.ccx_status = '803080001')) AS gos ON gos.ccx_project=proj.ccx_projectid

WHERE        (m.ccx_name LIKE '45b%') AND (m.ccx_datecompleted BETWEEN '2017/04/01' AND '2017/07/01') AND (m.ccx_status = '803080001') AND 
                         (proj.ccx_programme = 'D8943876-545A-E711-80FE-0050569FE3BD') AND (comp.tsg_companyuid NOT IN ('COMP00153968', 'COMP00091748', 'COMP00091284', 'COMP00177586', 'COMP00231427', 
                         'COMP00077428', 'COMP00077490')) AND (comp.accountid NOT IN
                             (SELECT        FilteredAccount_3.accountid
                               FROM            Filteredccx_milestone AS Filteredccx_milestone_3 INNER JOIN
                                                         Filteredccx_project AS Filteredccx_project_3 ON Filteredccx_milestone_3.ccx_project = Filteredccx_project_3.ccx_projectid INNER JOIN
                                                         FilteredAccount AS FilteredAccount_3 ON Filteredccx_project_3.ccx_customerid = FilteredAccount_3.accountid
                               WHERE        (Filteredccx_milestone_3.ccx_name LIKE '46%') AND (Filteredccx_milestone_3.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone_3.ccx_status = '803080001') AND 
                                                         (Filteredccx_project_3.ccx_programme = '04D0B1DF-FD36-E611-8870-005056A52209') OR
                                                         (Filteredccx_milestone_3.ccx_name LIKE '46%') AND (Filteredccx_milestone_3.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone_3.ccx_status = '803080001') AND 
                                                         (Filteredccx_project_3.ccx_programme = '14286237-FE36-E611-8870-005056A52209') OR
                                                         (Filteredccx_milestone_3.ccx_name LIKE '45b%') AND (Filteredccx_milestone_3.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone_3.ccx_status = '803080001') AND 
                                                         (Filteredccx_project_3.ccx_programme = 'D8943876-545A-E711-80FE-0050569FE3BD') OR
                                                         (Filteredccx_milestone_3.ccx_name LIKE '45b%') AND (Filteredccx_milestone_3.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone_3.ccx_status = '803080001') AND 
                                                         (Filteredccx_project_3.ccx_programme = '14286237-FE36-E611-8870-005056A52209') OR
                                                         (Filteredccx_milestone_3.ccx_name LIKE '45b%') AND (Filteredccx_milestone_3.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone_3.ccx_status = '803080001') AND 
                                                         (Filteredccx_project_3.ccx_programme = '04D0B1DF-FD36-E611-8870-005056A52209'))) OR

                         (m.ccx_name LIKE '45b%') AND (m.ccx_datecompleted BETWEEN '2017/04/01' AND '2017/07/01') AND (m.ccx_status = '803080001') AND 
                         (proj.ccx_programme = '14286237-FE36-E611-8870-005056A52209') AND (comp.tsg_companyuid NOT IN ('COMP00153968', 'COMP00091748', 'COMP00091284', 'COMP00177586', 'COMP00231427', 
                         'COMP00077428', 'COMP00077490')) AND (comp.accountid NOT IN
                             (SELECT        FilteredAccount_2.accountid
                               FROM            Filteredccx_milestone AS Filteredccx_milestone_2 INNER JOIN
                                                         Filteredccx_project AS Filteredccx_project_2 ON Filteredccx_milestone_2.ccx_project = Filteredccx_project_2.ccx_projectid INNER JOIN
                                                         FilteredAccount AS FilteredAccount_2 ON Filteredccx_project_2.ccx_customerid = FilteredAccount_2.accountid
                               WHERE        (Filteredccx_milestone_2.ccx_name LIKE '46%') AND (Filteredccx_milestone_2.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone_2.ccx_status = '803080001') AND 
                                                         (Filteredccx_project_2.ccx_programme = '04D0B1DF-FD36-E611-8870-005056A52209') OR
                                                         (Filteredccx_milestone_2.ccx_name LIKE '46%') AND (Filteredccx_milestone_2.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone_2.ccx_status = '803080001') AND 
                                                         (Filteredccx_project_2.ccx_programme = '14286237-FE36-E611-8870-005056A52209') OR
                                                         (Filteredccx_milestone_2.ccx_name LIKE '45b%') AND (Filteredccx_milestone_2.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone_2.ccx_status = '803080001') AND 
                                                         (Filteredccx_project_2.ccx_programme = 'D8943876-545A-E711-80FE-0050569FE3BD') OR
                                                         (Filteredccx_milestone_2.ccx_name LIKE '45b%') AND (Filteredccx_milestone_2.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone_2.ccx_status = '803080001') AND 
                                                         (Filteredccx_project_2.ccx_programme = '14286237-FE36-E611-8870-005056A52209') OR
                                                         (Filteredccx_milestone_2.ccx_name LIKE '45b%') AND (Filteredccx_milestone_2.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone_2.ccx_status = '803080001') AND 
                                                         (Filteredccx_project_2.ccx_programme = '04D0B1DF-FD36-E611-8870-005056A52209'))) OR

                         (m.ccx_name LIKE '45b%') AND (m.ccx_datecompleted BETWEEN '2017/04/01' AND '2017/07/01') AND (m.ccx_status = '803080001') AND 
                         (proj.ccx_programme = '04D0B1DF-FD36-E611-8870-005056A52209') AND (comp.tsg_companyuid NOT IN ('COMP00153968', 'COMP00091748', 'COMP00091284', 'COMP00177586', 'COMP00231427', 
                         'COMP00077428', 'COMP00077490')) AND (comp.accountid NOT IN
                             (SELECT        FilteredAccount_1.accountid
                               FROM            Filteredccx_milestone AS Filteredccx_milestone_1 INNER JOIN
                                                         Filteredccx_project AS Filteredccx_project_1 ON Filteredccx_milestone_1.ccx_project = Filteredccx_project_1.ccx_projectid INNER JOIN
                                                         FilteredAccount AS FilteredAccount_1 ON Filteredccx_project_1.ccx_customerid = FilteredAccount_1.accountid
                               WHERE        (Filteredccx_milestone_1.ccx_name LIKE '46%') AND (Filteredccx_milestone_1.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone_1.ccx_status = '803080001') AND 
                                                         (Filteredccx_project_1.ccx_programme = '04D0B1DF-FD36-E611-8870-005056A52209') OR
                                                         (Filteredccx_milestone_1.ccx_name LIKE '46%') AND (Filteredccx_milestone_1.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone_1.ccx_status = '803080001') AND 
                                                         (Filteredccx_project_1.ccx_programme = '14286237-FE36-E611-8870-005056A52209') OR
                                                         (Filteredccx_milestone_1.ccx_name LIKE '45b%') AND (Filteredccx_milestone_1.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone_1.ccx_status = '803080001') AND 
                                                         (Filteredccx_project_1.ccx_programme = 'D8943876-545A-E711-80FE-0050569FE3BD') OR
                                                         (Filteredccx_milestone_1.ccx_name LIKE '45b%') AND (Filteredccx_milestone_1.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone_1.ccx_status = '803080001') AND 
                                                         (Filteredccx_project_1.ccx_programme = '14286237-FE36-E611-8870-005056A52209') OR
                                                         (Filteredccx_milestone_1.ccx_name LIKE '45b%') AND (Filteredccx_milestone_1.ccx_datecompleted < '2017/04/01') AND (Filteredccx_milestone_1.ccx_status = '803080001') AND 
                                                         (Filteredccx_project_1.ccx_programme = '04D0B1DF-FD36-E611-8870-005056A52209')))

我建议这样做是因为您说如果 运行 在 SSRS 之外,SQL 可以正常工作。 SSRS 可能是语法问题,因此使用视图应该可以避免 SSRS 的复杂性。

当您有两个(或更多)具有相同名称的列时,通常会出现此错误。 SSMS 可以处理,但 SSRS 不能。看起来您有两列称为 expected grant(虽然可能会误读 SQL)尝试调用一个 anticipated grant2 并对任何其他重复的列名执行相同的操作,看看是否可行。

感谢大家的回复。 @tomdemaine 的回复让我觉得也许我可以去掉第二个 select 子句并使用 OR 代替 UNION 并且出于某种原因它已经起作用并且查询现在在 SSRS 和SQL 查询窗格,虽然它是一个很长的 WHERE 子句,但它在 13 秒内运行,这是可以接受的。