具有JOINED行数的一个字段的SUM计数

时间:2013-10-14 15:06:20

标签: mysql

我有以下三个表格(observationsaspect_labelsaspects):

观察表

结构

Observations table

数据

Observations data

方面标签表

结构

aspect_labels table

数据

aspect_labels data

方面表

结构

aspects table

数据

aspects data

这三个表背后的想法是每个observation都有一个Main_DevelopmentMain_Positive,但也有无数个额外的Positive方面和Development方面。

因此,例如,如果我现在输入观察(进入observations表),Main_Positive可能是Student Enthusiasmaspect_label ID 1)和{{ 1}}可能是Main_DevelopmentStudent Engagement ID 2)。不过,我还可以添加aspect_labelPositive ID 3)的标准Expectations方面,该方位进入aspect_label表。

我正在尝试按aspects分组,显示每位员工在观察中列出Teacher_ID每个aspect的次数。

所以,我想看到D Wraight有2 Student Engagement,0 Expectations,1 Student Enthusiasm等。

此查询与Main_Positive表中的observations方面完全相同,但我不知道如何将aspects表中的行添加到查询中。

SELECT
    CONCAT(sta.Firstname, " ", sta.Surname) AS `Member of Staff`,
    SUM(IF(o.`Main_Positive` = 1, 1, 0)) AS `Student enthusiasm`,
    SUM(IF(o.`Main_Positive` = 2, 1, 0)) AS `Student engagement`,
    SUM(IF(o.`Main_Positive` = 3, 1, 0)) AS `Expectations`,
    SUM(IF(o.`Main_Positive` = 4, 1, 0)) AS `Safe and supportive environment`,
    SUM(IF(o.`Main_Positive` = 5, 1, 0)) AS `Attitude and values of teacher`,
    SUM(IF(o.`Main_Positive` = 6, 1, 0)) AS `Objectives to stretch all students`,
  << MORE GOES HERE >>
FROM frog_observations.observations o
LEFT JOIN frog_shared.staff sta ON o.Teacher_ID = sta.ID
GROUP BY o.Teacher_ID
ORDER BY sta.Surname ASC, sta.Firstname ASC

查询产生此输出(当由PHPMA导出为CSV时):

Output

如何修改查询以包含aspects表中的联接行?

编辑:SQL小提琴:http://sqlfiddle.com/#!2/75cf3/1/0

1 个答案:

答案 0 :(得分:1)

你不能真正做到这一点,因为它会产生可变数量的列(除非你想在每次添加另一个方面时更改SQL)。

您可以这样做: -

SELECT CONCAT(s.Firstname, ' ', Surname), al.Title, IFNULL(SUM(AspectCnt), 0)
FROM staff s
CROSS JOIN aspect_labels al
LEFT OUTER JOIN
(
    SELECT o.Teacher_ID, o.Main_Positive AS AspectId, COUNT(*) AS AspectCnt
    FROM observations o
    GROUP BY o.Teacher_ID, o.Main_Positive
    UNION ALL
    SELECT o.Teacher_ID, o.Main_Development AS AspectId, COUNT(*) AS AspectCnt
    FROM observations o
    GROUP BY o.Teacher_ID, o.Main_Development
    UNION ALL
    SELECT o.Teacher_ID, a.Aspect_ID AS AspectId, COUNT(*) AS AspectCnt
    FROM observations o
    INNER JOIN aspects a
    ON o.ID = a.Observation_ID
    GROUP BY o.Teacher_ID, a.Aspect_ID
) Sub1
ON s.ID = Sub1.Teacher_ID
AND al.ID = Sub1.AspectId
GROUP BY CONCAT(s.Firstname, ' ', Surname), al.Title
ORDER BY CONCAT(s.Firstname, ' ', Surname), al.Title

每个方面的每个工作人员为您提供一行,然后在代码中循环以整理您的列。

编辑 - 使用固定列: -

SELECT CONCAT(s.Firstname, ' ', Surname), 
    IFNULL(SUM(IF(Sub1.AspectId = 1, 1, 0)), 0) AS `Student enthusiasm`,
    IFNULL(SUM(IF(Sub1.AspectId = 2, 1, 0)), 0) AS `Student engagement`,
    IFNULL(SUM(IF(Sub1.AspectId = 3, 1, 0)), 0) AS `Expectations`,
    IFNULL(SUM(IF(Sub1.AspectId = 4, 1, 0)), 0) AS `Safe and supportive environment`,
    IFNULL(SUM(IF(Sub1.AspectId = 5, 1, 0)), 0) AS `Attitude and values of teacher`,
    IFNULL(SUM(IF(Sub1.AspectId = 6, 1, 0)), 0) AS `Objectives to stretch all students`
FROM staff s
LEFT OUTER JOIN
(
    SELECT o.Teacher_ID, o.Main_Positive AS AspectId
    FROM observations o
    UNION ALL
    SELECT o.Teacher_ID, o.Main_Development AS AspectId
    FROM observations o
    UNION ALL
    SELECT o.Teacher_ID, a.Aspect_ID AS AspectId
    FROM observations o
    INNER JOIN aspects a
    ON o.ID = a.Observation_ID
) Sub1
ON s.ID = Sub1.Teacher_ID
GROUP BY CONCAT(s.Firstname, ' ', Surname)
ORDER BY CONCAT(s.Firstname, ' ', Surname)

将它分开以便使用教师ID进行分组来获取所有计数可能会更快,然后将结果返回到教师表以获取名称。会避免在连接字段上进行分组。