使用 MySQL 为交叉表拉入第二个值
Pulling in a second value for a Cross Tab with MySQL
我有以下查询...
SELECT COUNT(Department) as DeptCount, Survey.Department, Department.DepartmentName
FROM Survey JOIN Department ON Survey.Department = Department.DeptID
Where SurveyCompleted is not null GROUP BY DEPARTMENT ORDER BY Department;
唯一的问题是我用来计数的字段 "Department" 并不总是输入值(出于多种原因)。我可以使用另一个字段 Employee.DepartmentId,但我不确定如果我使用内联 IF 或类似结构如何创建相同的效果。
基本上我想看到的输出是:
DeptCount Department DepartmentName
43 2 Department1
68 9 Department2
83 13 Department3
现在我得到一个额外的行作为第一个结果
DeptCount Department DepartmentName
93 0 <blank>
43 2 Department1
68 9 Department2
83 13 Department3
如有任何帮助,我们将不胜感激。
谢谢。
如果你尝试这个会发生什么?通过使连接成为左连接,我们从 Survey
中获取所有记录,并从 Department
中获取任何匹配记录。然后我们可以聚合它们。
SELECT COUNT(Department) as DeptCount, coalesce(Survey.Department, 0)
, Department.DepartmentName
FROM Survey
left JOIN Department ON Survey.Department = Department.DeptID
Where SurveyCompleted is not null
GROUP BY DEPARTMENT ORDER BY Department;
顺便说一下,group by
会自动排序,所以如果您要分组,则不需要 order by
。
试试这个
SELECT COUNT(Department) as DeptCount, Survey.Department, Department.DepartmentName
FROM Survey JOIN Department ON Survey.Department = Department.DeptID
Where SurveyCompleted is not null and Survey.Department is not null and Survey.Department <> ''
GROUP BY DEPARTMENT
ORDER BY Department;
看来我想把它弄得太复杂了。
我需要做的就是命名从条件运算符创建的结果。然后我可以按该值分组......它有效(或似乎有效)。
这是我的查询结果:
SELECT COUNT(Department) as DeptCount,
IF(Survey.Department=0, Employee.DepartmentId, Survey.Department) As MyDept,
Department.DepartmentName
FROM Survey JOIN Department ON Survey.Department = Department.DeptID
JOIN Employee ON EmployeeId = Employee.Id
WHERE SurveyCompleted is not null
GROUP BY MyDept;
我有以下查询...
SELECT COUNT(Department) as DeptCount, Survey.Department, Department.DepartmentName
FROM Survey JOIN Department ON Survey.Department = Department.DeptID
Where SurveyCompleted is not null GROUP BY DEPARTMENT ORDER BY Department;
唯一的问题是我用来计数的字段 "Department" 并不总是输入值(出于多种原因)。我可以使用另一个字段 Employee.DepartmentId,但我不确定如果我使用内联 IF 或类似结构如何创建相同的效果。
基本上我想看到的输出是:
DeptCount Department DepartmentName
43 2 Department1
68 9 Department2
83 13 Department3
现在我得到一个额外的行作为第一个结果
DeptCount Department DepartmentName
93 0 <blank>
43 2 Department1
68 9 Department2
83 13 Department3
如有任何帮助,我们将不胜感激。
谢谢。
如果你尝试这个会发生什么?通过使连接成为左连接,我们从 Survey
中获取所有记录,并从 Department
中获取任何匹配记录。然后我们可以聚合它们。
SELECT COUNT(Department) as DeptCount, coalesce(Survey.Department, 0)
, Department.DepartmentName
FROM Survey
left JOIN Department ON Survey.Department = Department.DeptID
Where SurveyCompleted is not null
GROUP BY DEPARTMENT ORDER BY Department;
顺便说一下,group by
会自动排序,所以如果您要分组,则不需要 order by
。
试试这个
SELECT COUNT(Department) as DeptCount, Survey.Department, Department.DepartmentName
FROM Survey JOIN Department ON Survey.Department = Department.DeptID
Where SurveyCompleted is not null and Survey.Department is not null and Survey.Department <> ''
GROUP BY DEPARTMENT
ORDER BY Department;
看来我想把它弄得太复杂了。
我需要做的就是命名从条件运算符创建的结果。然后我可以按该值分组......它有效(或似乎有效)。
这是我的查询结果:
SELECT COUNT(Department) as DeptCount,
IF(Survey.Department=0, Employee.DepartmentId, Survey.Department) As MyDept,
Department.DepartmentName
FROM Survey JOIN Department ON Survey.Department = Department.DeptID
JOIN Employee ON EmployeeId = Employee.Id
WHERE SurveyCompleted is not null
GROUP BY MyDept;