SQL请求,按月分组的子选择的总和

时间:2017-09-01 16:17:27

标签: sql

我有3张桌子。为了使其更易于理解,我将其转换为一种简单的方式,就像不同的体育活动时间表一样。

1。 第一桌“体育”是不同的运动。 它包含了这项运动的身份和名称。

Table "sports"
+----+-------+
| id | Sport |
+----+-------+
| 1  | Judo  |
+----+-------+
| 2  | Boxe  |
+----+-------+

2。 第二个表“类”是实际发生的不同类。每个班级都与运动和约会有关。 它包含了id,类的日期,它所关联的运动的id,以及使它独特的唯一ID,结合了日期和运动id。

table "classes"
+----+----------+------------+--------------+
| id | id_sport | dates      | unique_key   |
+----+----------+------------+--------------+
| 1  | 1        | 2017-03-10 | 1_2017-03-10 |
+----+----------+------------+--------------+
| 2  | 2        | 2017-03-10 | 2_2017-03-10 |
+----+----------+------------+--------------+
| 3  | 2        | 2017-03-17 | 2_2017-03-17 |
+----+----------+------------+--------------+

3。 第三个表“约会”收集每个存在。 它包含来的人的姓名,运动的身份以及班级的唯一钥匙。我知道,在这个表中,列id_sport不是必需的,因为我们可以在表类中找到它。

Table "appointments"
+----+---------+----------+--------------+
| id | student | id_sport | id_class     |
+----+---------+----------+--------------+
| 1  | Tom     | 1        | 1_2017-03-10 |
+----+---------+----------+--------------+
| 2  | Sam     | 1        | 1_2017-03-10 |
+----+---------+----------+--------------+
| 3  | Mat     | 2        | 2_2017-03-10 |
+----+---------+----------+--------------+
| 4  | Mat     | 2        | 2_2017-03-17 |
+----+---------+----------+--------------+
| 5  | Tom     | 2        | 2_2017-03-10 |
+----+---------+----------+--------------+

我正在尝试排序一些统计数据。我试着每个月找到参加某项特定运动的人数。

有了这个请求,我试图找到在boxe类中出现的人数:

SELECT
MONTH(classes.dates) AS Month,
  (SELECT count(*)
   FROM appointments
   WHERE appointments.id_sport = sports.id AND appointments.id_class = 
classes.unique_key AND sports.id = 2 LIMIT 1) AS nb_appointment   
FROM sports
INNER JOIN classes ON classes.id_sport = sports.id
WHERE sports.id = 2
ORDER BY Month

结果是

+-------+---------------+
| Month | nb_appointment|
+-------+---------------+
| 3     | 2             |
+-------+---------------+
| 3     | 1             |
+-------+---------------+

我想要的是以下内容:第3个月=> nb_appointment 3

如果我还有一些月份4,5等,它也会显示这个月的预约总额。

我尝试按月分组,但它没有预约的次数,只显示最后一次......

有什么想法吗?

这是我试过的sql小提琴 http://sqlfiddle.com/#!9/733782/1

我也尝试在SUM中包装select,但似乎仍然不起作用:http://sqlfiddle.com/#!9/2ae510/1

我开始使用php / sql,抱歉,如果这个问题看起来很愚蠢。

3 个答案:

答案 0 :(得分:1)

select count(distinct student) , Month(c.dates)
   from appointment a 
   inner join classes c
   on a.id_class = c.unique_key
   where c.id_sports = 2
group by Month(c.date)

答案 1 :(得分:0)

你实际上可以包装" SUM"围绕您的子查询:

SELECT
MONTH(classes.dates) AS Month,  
SUM(
(SELECT count(*)
   FROM appointment
   WHERE appointment.id_sport = sports.id 
   AND appointment.id_classes = classes.unique_key 
   AND sports.id = 2 LIMIT 1) 
) AS nb_appointment   
FROM sports
INNER JOIN classes ON classes.id_sport = sports.id
WHERE sports.id = 2
ORDER BY Month

这给出了答案3(你的帖子说你期望nb_appointments为5,但我认为它是3 ??)

答案 2 :(得分:0)

好的,最后它通过包装sum()并添加一个组来实现,如下所示:

SELECT
MONTH(classes.dates) AS Month,
SUM(
  (SELECT count(*)
   FROM appointment
   WHERE appointment.id_sport = sports.id AND appointment.id_classes = 
classes.unique_key AND sports.id = 2 LIMIT 1)
  )AS nb_appointment   
FROM sports
INNER JOIN classes ON classes.id_sport = sports.id
WHERE sports.id = 2
GROUP BY Month
ORDER BY Month

谢谢!