0

我正在为似乎是我写过的最复杂的 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`

我想要达到的目标

我想列出一份我们在特定观察方面最好的老师的名单。例如,我想看看我的前 5 位老师的“提问”。

最重要的是,我想对数据进行一些过滤:

  • 正式(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 positivesa weighted total)之外,我不知道如何编写我认为将是子查询的内容,以便从单个语句中获取所有这些信息。

任何指导表示赞赏。


编辑:示例输入/输出

输入 用户想要查看方面的前 5 名员工Questioning

流程 - 方面在表中Questioning有 ID 。- 然后,系统应为每位员工计算他们拥有的人数,并设置为。其中一些观察将是正式的,而一些将是SBP。- 系统还应计算表中每个员工所在的行数。4aspect_labelsobservationsMain_Positive_Aspect4TypeTypeaspectsAspect_ID4

输出

钥匙:

  • FMP = Formalobservations表中的行数;; 和是(即在表中)TypeGREATEST(Achievement_Grade, Behaviour_Grade, Teaching_Grade) BETWEEN 1 AND 2Main_Positive_Aspect4Questioningaspects_label
  • SMP =observations表中TypeSBP 和Main_Positive_Aspectis的行数4
  • 其他=aspects表中Aspect_ID每个4员工的行数(通过o.Observation_ID->o.Staff_ID链接)
  • 点数= 每项的加权总和 - 每个 FMP 值 3 分,每个 SMP 值 2 分,每个 Other 值 1。然后应DESC按此列对查询进行排序。

示例输出:

-------------------------------------------------------------
|       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  #
#=======#

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
4

1 回答 1

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
于 2013-06-19T15:36:43.933 回答