我有四张桌子,我需要找到特定身份证的每个分数的平均值。我不需要整个列的平均值,而是每个表中具有相同id的每个记录的平均值。 我试过这个:
SELECT DISTINCT M.system_id, S.name, SUM(A.Score + WAL.score + F.score + WIN.score) / 4
AS avgScore
FROM dbo.T3_MovementSystemJoin AS M
INNER JOIN dbo.T3_systems AS S ON M.system_id = S.id
INNER JOIN T3_ApplicationSystemJoin AS A ON A.Application_id = @application_id
INNER JOIN T3_WallTypeSystemJoin AS WAL ON WAL.wall_id = @wall_id
INNER JOIN T3_FenestrationSystemJoin AS F ON F.fenestration_id = @fen_id
INNER JOIN T3_WindowOrientation_System AS WIN ON WIN.window_id = @window_id
INNER JOIN T3_ConstructionSystemJoin AS C ON C.contruction_id = @construction_id
INNER JOIN T3_JointDepthSystemJoin AS J ON J.JointDepth_id = @JointDepth_id
INNER JOIN T3_JointGapSystemJoin AS JG ON JG.JointGap_id = @JointGap_id
WHERE (M.movement_id = @movement_id)
GROUP BY M.System_id, S.name
感谢您的帮助!
答案 0 :(得分:1)
不需要Sum
(也没有分组)
SELECT DISTINCT M.system_id, S.name, (IsNull(A.Score, 0) + IsNull(WAL.score, 0) + IsNull(F.score, 0) + IsNull(WIN.score, 0)) /4
as avgscore
FROM dbo.T3_MovementSystemJoin AS M
INNER JOIN dbo.T3_systems AS S ON M.system_id = S.id
INNER JOIN T3_ApplicationSystemJoin AS A ON A.Application_id = @application_id
INNER JOIN T3_WallTypeSystemJoin AS WAL ON WAL.wall_id = @wall_id
INNER JOIN T3_FenestrationSystemJoin AS F ON F.fenestration_id = @fen_id
INNER JOIN T3_WindowOrientation_System AS WIN ON WIN.window_id = @window_id
INNER JOIN T3_ConstructionSystemJoin AS C ON C.contruction_id = @construction_id
INNER JOIN T3_JointDepthSystemJoin AS J ON J.JointDepth_id = @JointDepth_id
INNER JOIN T3_JointGapSystemJoin AS JG ON JG.JointGap_id = @JointGap_id
WHERE (M.movement_id = @movement_id)
答案 1 :(得分:0)
SELECT DISTINCT M.system_id
,S.name
,(ISNULL(A.Score,0) + ISNULL(WAL.score,0) + ISNULL(F.score,0) + ISNULL(WIN.score,0)) /4 as 'AvgScore'
FROM dbo.T3_MovementSystemJoin AS M
INNER JOIN dbo.T3_systems AS S ON M.system_id = S.id
INNER JOIN T3_ApplicationSystemJoin AS A ON A.Application_id = @application_id
INNER JOIN T3_WallTypeSystemJoin AS WAL ON WAL.wall_id = @wall_id
INNER JOIN T3_FenestrationSystemJoin AS F ON F.fenestration_id = @fen_id
INNER JOIN T3_WindowOrientation_System AS WIN ON WIN.window_id = @window_id
INNER JOIN T3_ConstructionSystemJoin AS C ON C.contruction_id = @construction_id
INNER JOIN T3_JointDepthSystemJoin AS J ON J.JointDepth_id = @JointDepth_id
INNER JOIN T3_JointGapSystemJoin AS JG ON JG.JointGap_id = @JointGap_id
WHERE (M.movement_id = @movement_id)
答案 2 :(得分:0)
如果您不希望NULL值变为零并包含在平均值中:
SELECT DISTINCT M.system_id, S.name, X.avgScore
FROM dbo.T3_MovementSystemJoin AS M
INNER JOIN dbo.T3_systems AS S ON M.system_id = S.id
INNER JOIN T3_ApplicationSystemJoin AS A ON A.Application_id = @application_id
INNER JOIN T3_WallTypeSystemJoin AS WAL ON WAL.wall_id = @wall_id
INNER JOIN T3_FenestrationSystemJoin AS F ON F.fenestration_id = @fen_id
INNER JOIN T3_WindowOrientation_System AS WIN ON WIN.window_id = @window_id
INNER JOIN T3_ConstructionSystemJoin AS C ON C.contruction_id = @construction_id
INNER JOIN T3_JointDepthSystemJoin AS J ON J.JointDepth_id = @JointDepth_id
INNER JOIN T3_JointGapSystemJoin AS JG ON JG.JointGap_id = @JointGap_id
CROSS APPLY ( SELECT AVG(s) FROM (VALUES (A.Score),(WAL.score),(F.score),(WIN.score) ) scores(s) ) X(avgScore)
WHERE (M.movement_id = @movement_id)
GROUP BY M.System_id, S.name