查询中的MSSQL Group By Error

时间:2014-08-21 06:38:06

标签: sql-server

我添加了Project_Contract.ContractorName但查询得到了错误。 消息156,级别15,状态1,行18关键字“GROUP”附近的语法不正确。

select top (100) percent dbo.Project_Profile.Project_ID
    ,dbo.Project_Contract.ContractorName
    ,dbo.Schemes.Scheme_Name as [Name of Project]
    ,dbo.Program_Ctrl.Program_Title as [Contract Status]
    ,dbo.District_Ctrl.District_Name as District
    ,M.Module_Title as Module
    ,(
        select MAX(Progress_Percentage) as Expr1
        from dbo.Progress_Reporting as PR
        where (M.ModuleID = Module_Id)
            and (dbo.Milestone_Ctrl.Milestone_ID = Project_Milestone_ID)
            and (Progress_Type = 'A')
        ) as Milestone_Percent_Share
    ,dbo.Milestone_Ctrl.Milestone_Title
from dbo.District_Ctrl
inner join dbo.Project_Profile
inner join dbo.Project_Profile
inner join dbo.Project_Contract
    on dbo.Project_Profile.Project_ID = dbo.Project_Contract.ProjectId
inner join dbo.Schemes
    on dbo.Project_Profile.Scheme_id = dbo.Schemes.Scheme_id
inner join dbo.Program_Ctrl
    on dbo.Project_Profile.Program_ID = dbo.Program_Ctrl.Program_ID
inner join dbo.Project_Modules
    on dbo.Project_Profile.Project_ID = dbo.Project_Modules.ProjectId
        on dbo.District_Ctrl.District_ID = dbo.Schemes.District_ID inner join dbo.Modules_Ctrl as M
        on M.ModuleID = dbo.Project_Modules.ModuleID 
inner join dbo.Milestone_Ctrl
inner join dbo.Project_Milestones
    on dbo.Milestone_Ctrl.Milestone_ID = dbo.Project_Milestones.Project_Milestone_ID
        on M.ModuleID = dbo.Project_Milestones.ModuleId 
group by dbo.Project_Profile.Project_ID
        ,dbo.Project_Contract.ContractorName
        ,dbo.Schemes.Scheme_Name
        ,dbo.Program_Ctrl.Program_Title
        ,M.Module_Title
        ,dbo.Project_Modules.ProjectModuleId
        ,dbo.Project_Milestones.Project_Milestone_ID
        ,dbo.District_Ctrl.District_Name
        ,dbo.Project_Milestones.Milestone_Percent_Share
        ,dbo.Project_Milestones.MileStoneId
        ,dbo.Milestone_Ctrl.Milestone_ID
        ,dbo.Milestone_Ctrl.Milestone_Title
        ,M.ModuleID
order by dbo.Project_Profile.Project_ID

2 个答案:

答案 0 :(得分:1)

您有两个加入Project_Profile表并且此加入只有一个ON,您必须添加另一个ON来解决您的问题或删除dbo.Project_Profile INNER JOIN部分命令。

答案 1 :(得分:0)

下面:

                         dbo.Project_Profile INNER JOIN
                         dbo.Project_Profile INNER JOIN

没有别名的重复Project_Profile