子查询从一个查询中的两个表中选择多个COUNT

时间:2013-06-19 14:36:25

标签: mysql

我正在努力解决我曾写过的最复杂的SQL查询!

数据库结构

frog_shared.staff
`ID`, `Firstname`, `Surname`

frog_observations.observations
`ID`, `Teacher_ID`, `Type`, `Main_Positive_Aspect`, `Main_Development_Aspect`, `Grade_for_Behaviour`, `Grade_for_Attainment`, `Grade_for_Teaching`

frog_observations.aspects
`ID`, `Observation_ID`, `Label_ID`, `Type`

frog_observations.aspect_labels
`ID`, `Title`

示例数据

Member of staff:
`12345`, `Duncan`, `Wraight`

Observation:
`9888`, `12345`, `Formal`, `5`, `7`, `1`, `1`, `1`

Aspects:
`101`, `9888`, `2`, `P`

Aspect labels:
`2`, `Questioning`

我想要实现的目标

我想在特定的观察方面列出我们最好的老师名单。例如,我希望看到我的前五名教师“提问”。

最重要的是,我想稍微过滤一下数据:

  • 应计算正式(Type=Formal)和分享最佳做法(Type=SBP)观察的方面
  • 正式观察的方面只应计算,如果观察结果的评分最高为2(不高,即两个等级不是2,一个是3),但分享最佳实践观察的结果均为0等级
  • 观察表中记录的主要积极方面应加权计数
  • 对于正式观察,主要的积极方面应加权为正常方面的3 *
  • 为了分享最佳实践观察,主要的积极方面应该加权为正常方面的2 *

我的尝试

此声明按其等级过滤观察结果,但不包括任何主要的积极方面(它们未包含在原始系统中)

SELECT
  CONCAT(s.Firstname, " ", s.Surname) AS `Teacher`,
  COUNT( * ) AS Total,
  GREATEST(o.`Achievement_Grade`, o.`Behaviour_Grade`, o.`Teaching_Grade`) AS `Worst Grade`
FROM frog_observations.observations o
  INNER JOIN frog_shared.staff s ON o.Teacher_ID = s.ID
  INNER JOIN frog_observations.aspects a ON o.ID = a.Observation_ID
WHERE a.Aspect_ID =  4
  AND ( GREATEST(o.`Achievement_Grade`, o.`Behaviour_Grade`, o.`Teaching_Grade`) BETWEEN 1 AND 2 ) AND o.Datetime > '2011-09-01'
  AND a.Type =  'P'
GROUP BY s.ID
ORDER BY `Total` DESC 
LIMIT 5

现在我想做的事情是这样的:

SELECT
  CONCAT(s.Firstname, " ", s.Surname) AS `Teacher`,
  COUNT(mp.*) AS `Number of Appraisal Main Positives`,
  COUNT(sp.*) AS `Number of SBP Main Positives`,
  COUNT(a.*) AS `Number of Other Positives`,
  SUM(`Number of Appraisal Main Positives` + `Number of SBP Main Positives` `Number of Other Positives`) AS `Total`
FROM
  `frog_observations`.`observations` o,
  ( `frog_observations`.`observations` WHERE `Type` = 'Formal') AS mp,
  ( `frog_observations`.`observations` WHERE `Type` = 'SBP') AS sp
INNER JOIN
  `frog_observations`.`aspects` a ON a.Observation_ID = o.ID
GROUP BY s.ID

不幸的是,除了知道我想要的标题(即number of appraisal main positivesnumber of sharing best practice positivesnumber of standard positivesweighted total)之外,我不知道如何写出我认为的内容将是子查询以从单个语句中获取所有这些信息。

任何指导意见。


编辑:示例输入/输出

输入 用户希望查看方面Questioning

的前5名员工

过程   - 方面Questioning4表格中具有ID aspect_labels。   - 系统应该为每位员工计算observations设置为Main_Positive_Aspect 4时的Type。其中一些观察结果为Type正式,有些将为aspects SBP。   - 系统还应该为Aspect_ID 4 observations的每位员工计算Type表中的行数。

输出

键:

  • FMP = GREATEST(Achievement_Grade, Behaviour_Grade, Teaching_Grade) BETWEEN 1 AND 2表格中Main_Positive_Aspect为正式的行数; 4;并且Questioningaspects_label(即observations表中的Type
  • SMP = Main_Positive_Aspect表格中4为SBP且aspectsAspect_ID
  • 的行数
  • 其他 = 4表格中每位员工o.Observation_IDo.Staff_ID的行数(通过DESC - &gt ; ------------------------------------------------------------- | Staff Name FMP SMP Other Points | |------------------------------------------------------------ | D Wraight 2 1 4 12 | | A Nother 3 0 0 9 | | J Bloggs 0 4 1 9 | | J Arthur 1 1 1 6 | | M Turner 0 1 0 2 | ------------------------------------------------------------- 链接)
  • 积分 =每个积分的加权总数 - 每个FMP值3分,每个SMP值2分,每个其他值1个。然后应该查询查询,#=======# # FMP # #=======# SELECT CONCAT(s.Firstname, " ", s.Surname) AS `Teacher`, COUNT( * ) AS `FMP` FROM frog_observations.observations o INNER JOIN frog_shared.staff s ON o.Teacher_ID = s.ID WHERE o.Main_Positive = 4 AND o.Type = 'F' AND ( GREATEST(o.`Achievement_Grade`, o.`Behaviour_Grade`, o.`Teaching_Grade`) BETWEEN 1 AND 2 ) GROUP BY s.ID #=======# # SMP # #=======# SELECT CONCAT(s.Firstname, " ", s.Surname) AS `Teacher`, COUNT( * ) AS `SMP` FROM frog_observations.observations o INNER JOIN frog_shared.staff s ON o.Teacher_ID = s.ID WHERE o.Main_Positive = 4 AND o.Type = 'S' GROUP BY s.ID #=========# # Other # #=========# SELECT CONCAT(s.Firstname, " ", s.Surname) AS `Teacher`, COUNT( * ) AS `Other` FROM observations o INNER JOIN frog_shared.staff s ON o.Teacher_ID = s.ID INNER JOIN aspects a ON o.ID = a.Observation_ID WHERE a.Aspect_ID = 4 AND a.Type = 'P' GROUP BY s.ID ,通过这一栏。

示例输出:

{{1}}

以下是单独编写的查询,这些查询适用于我的数据库。基本上我需要将这些合并为一个查询。如果可能的话!

{{1}}

1 个答案:

答案 0 :(得分:2)

这样的事情是否可以用于初步裁剪......

SELECT
  CONCAT(s.Firstname, " ", s.Surname) AS Teacher,
  sum(Case when `Type` = 'Formal' then 1 else 0 end) AS `Number of Appraisal Main Positives`,
  sum(Case when `Type` = 'SBP' then 1 else 0 end)  AS `Number of SBP Main Positives`,
  coalesce(OtherPositives,0) AS `Number of Other Positives`,
  sum(Case when `Type` in ( 'Formal' ,'SBP') then 1 else 0 end) + coalesce(OtherPositives,0)  AS `Total`
FROM observations o
INNER JOIN staff s ON o.Teacher_ID = s.ID
LEFT JOIN (select Observation_ID, count(*) as OtherPositives) from aspects where Label_ID=4 group by Observation_ID) a ON a.Observation_ID = o.ID
WHERE Main_Positive =4
GROUP BY s.ID