使用多个列和已连接的列创建公式查询

时间:2014-03-11 12:58:01

标签: mysql

我正在尝试制作一个公式,让学生的奖励积分与他们的消极行为标志相对应。

学生获得LEAP积分(在transactions表中)以获得积极的行为。他们根据奖励的类别获得更多积分,即Model Citizen给予学生10分。

另一方面,学生会因为负面行为而获得单一旗帜。然后在数据库表中对Flag的类别进行加权,即Aggressive Defiance类别的权重高4,而Low Level Disruption只有 { {1}}。

因此,困难在于试图考虑旗帜类别的权重。它们存储在1列下的categories表中。


以下是SQL小提琴:http://sqlfiddle.com/#!2/2e5756


在我看来,伪SQL代码看起来像这样......

Weight

然而,我不知道如何在MySQL中实现SELECT CONCAT( stu.Surname, ", ", stu.Firstname ) AS `Student`, SUM(t.Points) AS `LEAP Points`, SUM(<<formula>>) AS `Flags` ( `LEAP Points` - `Flags` ) AS `Worked Out Points Thing` FROM student stu LEFT JOIN transactions t ON t.Recipient_ID = stu.id LEFT JOIN flags f ON f.Student_ID = stu.id LEFT JOIN categories c ON f.Category_ID = c.ID GROUP BY stu.id 。它必须是这样的:

<<formula>>

所以,如果学生有这些旗帜......

SUM OF[ Each of Student's Flags * that Flag's Category Weighting ]

他们在#1 f.Reason "Being naughty", f.Category_ID "1", c.Title "Low Level Disruption", c.Weight "1" #1 Reason "Aggressively naughty!", Category "Aggressive Defiance", Category Weighting "4" #1 Reason "Missed detention", Category "Missed Detention", Category Weighting "3" 等式中总共需要1+4+3 = 9个点。

因此,所需的输出基本上是......

Worked Out Points Thing

从上面的SQL小提琴,这里是必需的输出..我错过了一些学生,因为我必须手动解决这些问题:

Student         LEAP Points     Flags Equation Points       LEAP Points minus Flag Points
D Wraight       1000            800                         200
D Wraight2      500             800                         -300
D Wraight3      1200            300                         900

这将有助于我们在查看年终奖励旅行时,为每位学生提供选择。

希望这是有道理的...有点难以解释它...

提前致谢,

2 个答案:

答案 0 :(得分:1)

找出你的公式&#39;首先是因为它是最深的部分。向外工作。 建立一个标志*每个学生的重量表

select sum(weight), student_id from flags f
join categories c
on f.category_id = c.id
group by student_id

所以现在你已经从每个学生的交易总数中得到一个标志值表减去

select sum(points), recipient_id from transactions
group by recipient_id

所以现在我们有两个表,其中包含学生ID的正值和负值(显然假设学生ID是收件人ID) 你希望那些有事务但没有标志的人出现在结果中,所以外连接。 并且数字减去null为null所以如果ifnull函数在标志上得到0

select a.student, points - ifnull(penalties, 0) as netPoints
from

(select sum(points) as points, recipient_id as student from transactions
group by student) as a

left outer join

(select sum(weight) as penalties, student_id as student from flags f
join categories c
on f.category_id = c.id
group by student) as b

on

a.student = b.student

所以在那里的名字只是

select
concat(firstname, ', ', surname) as name,
ifnull(points,0) as totalPoints,
ifnull(penalties,0) as totalPenalties,
ifnull(points,0) - ifnull(penalties, 0) as netPoints,
ifnull(countFlags, 0)

from
student
left join

(select sum(points) as points, recipient_id as student from transactions
group by student) as a

on student.id = a.student

left join

(select sum(weight) as penalties, count(f.id) as countFlags, student_id as student from flags f
join categories c
on f.category_id = c.id
group by student) as b

on

student.id = b.student

加入条件始终来自学生的id列,它永远不会为空。 可能有更有效的方式,但是谁在乎呢?

答案 1 :(得分:0)

回到问题(冒着重复自己的风险!),给定以下数据集,期望的结果集会是什么样的......

SELECT * FROM flags;
+------+------------+----------+---------------------+-----------+-------------+--------------------------+---------------------+
| ID   | Student_ID | Staff_ID | Datetime            | Period_ID | Category_ID | Action_Taken_Category_ID | Action_Taken_Status |
+------+------------+----------+---------------------+-----------+-------------+--------------------------+---------------------+
| 8843 |     137608 |    35003 | 2014-03-11 08:31:00 |         8 |          16 |                        7 | P                   |
| 8844 |     137608 |    35003 | 2014-03-11 08:31:00 |         8 |          16 |                        7 | P                   |
| 8845 |     139027 |    35003 | 2014-03-11 08:31:00 |         8 |          16 |                        7 | P                   |
| 8846 |     139041 |    35003 | 2014-03-11 08:31:00 |         8 |          16 |                        7 | P                   |
| 8847 |     139041 |    34961 | 2014-03-11 09:01:02 |         2 |          12 |                       26 | P                   |
| 8848 |     139041 |    34996 | 2014-03-11 09:23:21 |         3 |          12 |                       27 | C                   |
| 8849 |     139041 |    35022 | 2014-03-11 11:07:46 |         4 |          34 |                       28 | P                   |      
| 8850 |     139892 |   138439 | 2014-03-11 11:12:47 |         4 |          21 |                        7 | C                   |
| 8851 |     138832 |   138439 | 2014-03-11 11:12:48 |         4 |          21 |                        7 | C                   |
| 8852 |      34533 |   138439 | 2014-03-11 11:12:48 |         4 |          21 |                        7 | C                   |
+------+------------+----------+---------------------+-----------+-------------+--------------------------+---------------------+

SELECT * FROM categories;
+----+------+--------------------------------------+--------+----------+
| ID | Type | Title                                | Weight | Added_By |
+----+------+--------------------------------------+--------+----------+
| 10 | F    | Low level disruption                 |      1 |     NULL |
| 11 | F    | Swearing directly at another student |      2 |     NULL |
| 12 | F    | Late                                 |      1 |     NULL |
| 13 | F    | Absconded                            |      3 |     NULL |
| 14 | F    | Refusal to follow instruction        |      3 |     NULL |
| 15 | F    | Smoking                              |      2 |     NULL |
| 16 | F    | No homework                          |      2 |     NULL |
| 17 | F    | Disruptive outside classroom         |      2 |     NULL |
| 18 | F    | Eating/drinking in lesson            |      1 |     NULL |
| 19 | F    | Incorrect uniform/equipment          |      1 |     NULL |
| 20 | F    | Phone out in lesson                  |      3 |     NULL |
| 21 | F    | Aggressive defiance                  |      4 |     NULL |
| 22 | F    | Missed detention                     |      3 |     NULL |
| 23 | F    | Inappropriate behaviour/comments     |      3 |     NULL |
| 32 | F    | IT Misuse                            |   NULL |     NULL |
| 34 | F    | Inappropriate attitude towards staff |   NULL |     NULL |
| 35 | F    | Care & Guidance                      |   NULL |     NULL |
+----+------+--------------------------------------+--------+----------+

SELECT * FROM transactions;
+----------------+------------+----------+--------------+--------+-------------+
| Transaction_ID | Datetime   | Giver_ID | Recipient_ID | Points | Category_ID |
+----------------+------------+----------+--------------+--------+-------------+
|             34 | 2011-09-07 |    35019 |       137608 |      2 |           1 |
|             35 | 2011-09-07 |    35019 |       139027 |      2 |           1 |
|             36 | 2011-09-07 |    35019 |       139041 |      2 |           1 |
|             37 | 2011-09-07 |    35019 |       139041 |      2 |           1 |
|             38 | 2011-09-07 |    35019 |       139041 |      2 |           1 |
|             39 | 2011-09-07 |    35019 |       139041 |      2 |           1 |
|             40 | 2011-09-07 |    35019 |       137434 |      2 |           1 |
|             41 | 2011-09-07 |    35019 |       137434 |      2 |           1 |
|             42 | 2011-09-07 |    35019 |       137434 |      2 |           1 |
|             43 | 2011-09-07 |    35019 |       137434 |      2 |           1 |
|             44 | 2011-09-07 |    35006 |       137434 |      2 |           1 |
|             45 | 2011-09-07 |    35006 |        90306 |      2 |           1 |
|             46 | 2011-09-07 |    35006 |        90306 |      2 |           1 |
|             47 | 2011-09-07 |    35006 |        90306 |      2 |           1 |
|             48 | 2011-09-07 |    35023 |       137608 |      5 |           2 |
|             49 | 2011-09-07 |    35023 |       139027 |      5 |           2 |
|             50 | 2011-09-07 |    35023 |       139564 |      5 |           2 |
|             51 | 2011-09-07 |    35023 |       139564 |      5 |           2 |
|             52 | 2011-09-07 |    35023 |       139564 |      5 |           2 |
|             53 | 2011-09-07 |    35023 |       137608 |      5 |           3 |
+----------------+------------+----------+--------------+--------+-------------+

SELECT id,UPN,Year_Group,Tutor_Group,SEN_Status,Flags FROM student;
+--------+---------------+------------+-------------+------------+--------+
| id     | UPN           | Year_Group | Tutor_Group | SEN_Status | Flags  |
+--------+---------------+------------+-------------+------------+--------+
| 137608 | A929238400044 |         11 | 11VID       | A          |        |
| 139027 | A929238401045 |         10 | 10KS        |            |        |
| 139041 | A929238402017 |         10 | 10RJ        | A          | FSM    |
| 139892 | A929238403018 |          9 | 9BW         |            |        |
| 139938 | A929238403020 |          9 | 9RH         |            |        |
| 137434 | A929238500027 |         11 | 11VID       |            |        |
| 138832 | A929238502002 |         10 | 10RY        | A          | FSM,PA |
| 34533  | A929238599028 |          0 |             |            | PA     |
| 139564 | A929241500025 |         12 |             |            | PA     |
| 90306  | A929253100006 |         12 | SLH         | A          | PA     |
+--------+---------------+------------+-------------+------------+--------+