Wrong SQL Aggregate result

时间:2018-10-02 09:11:57

标签: sql sql-server

I want to get average of a tuple. But my query won't get the result. I cannot find the mistake after hours of research. Please help

MY Query:

SELECT  dbo.Objectives.Id,
    dbo.Objectives.Clause, 
    dbo.Objectives.FrequencyId, 
    ObjectiveFrequency.Title AS ObjectiveFrequencyTitle,                
    dbo.Objectives.ObjectiveCategoryId, 
    dbo.ObjectiveCategories.Category AS ObjectiveCategory, 
    dbo.Objectives.Target, 
    dbo.Objectives.TargetUnitId, 
    TargetUOM.Unit AS TargetUnit, 
    TargetUOM.Symbole AS TargetSymbol, 
    TargetUOM.Position AS TargetPosition,               
    AVG(dbo.Achievements.ActualResult) AS AverageResult,
    dbo.Achievements.ActualResultUOMId, 
    ActualResultUOM.Unit AS ActualResultUOMUnit, 
    ActualResultUOM.Symbole AS ActualResultUOMSymbole, 
    ActualResultUOM.Position AS ActualResultUOMPosition 
FROM dbo.Objectives
    LEFT OUTER JOIN dbo.Achievements ON dbo.Objectives.Id =   dbo.Achievements.ObjectiveId
    INNER JOIN dbo.UnitOfMeasures AS TargetUOM ON TargetUOM.Id = dbo.Objectives.TargetUnitId                         
    INNER JOIN dbo.Frequencies AS ObjectiveFrequency ON dbo.Objectives.FrequencyId = ObjectiveFrequency.Id 
    INNER JOIN dbo.Employees AS AssignEmployee ON dbo.Objectives.AssignToEmployeeId = AssignEmployee.EmployeeId 
    INNER JOIN dbo.Departments AS AssignDepartment ON dbo.Objectives.AssignedDepartmentDepartmentId = AssignDepartment.DepartmentId 
    LEFT OUTER JOIN dbo.ObjectiveCategories ON dbo.Objectives.ObjectiveCategoryId = dbo.ObjectiveCategories.Id  
    LEFT OUTER JOIN dbo.UnitOfMeasures ValueUOM ON    dbo.Achievements.ValueUOMId = ValueUOM.Id
    LEFT OUTER JOIN dbo.UnitOfMeasures VaryValuUOM ON  dbo.Achievements.VaryValueUOMId = VaryValuUOM.Id 
    LEFT OUTER JOIN dbo.UnitOfMeasures ActualResultUOM ON dbo.Achievements.ActualResultUOMId = ActualResultUOM.Id 
    LEFT OUTER JOIN dbo.UnitOfMeasures AchievementUOM ON dbo.Achievements.ObjectiveAchievementUOMId = AchievementUOM.Id
GROUP BY dbo.Objectives.Id,
    dbo.Achievements.ActualResult, 
    dbo.Objectives.Clause, 
    dbo.Objectives.FrequencyId, 
    ObjectiveFrequency.Title,               
    dbo.Objectives.ObjectiveCategoryId, 
    dbo.ObjectiveCategories.Category, 
    dbo.Objectives.Target, 
    dbo.Objectives.TargetUnitId, 
    TargetUOM.Unit, 
    TargetUOM.Symbole, 
    TargetUOM.Position,                 
    dbo.Achievements.ActualResultUOMId, 
    ActualResultUOM.Unit, 
    ActualResultUOM.Symbole, 
    ActualResultUOM.Position 

OUTPUT enter image description here

I want to get average of actual result according to objectId. Please help.

1 个答案:

答案 0 :(得分:2)

您正在对Achievements.ActualResult求平均值,但是将其添加到GROUP BY子句中。请按照以下说明从GROUP BY中将其删除:

GROUP BY dbo.Objectives.Id,
    dbo.Objectives.Clause, 
    dbo.Objectives.FrequencyId, 
    ObjectiveFrequency.Title,               
    dbo.Objectives.ObjectiveCategoryId, 
    dbo.ObjectiveCategories.Category, 
    dbo.Objectives.Target, 
    dbo.Objectives.TargetUnitId, 
    TargetUOM.Unit, 
    TargetUOM.Symbole, 
    TargetUOM.Position,                 
    dbo.Achievements.ActualResultUOMId, 
    ActualResultUOM.Unit, 
    ActualResultUOM.Symbole, 
    ActualResultUOM.Position