给出一个日期为1的事件列表,其中1表示某人(例如学生)加入了一个组,-1表示某物离开了一个组,是否可以在SQL中按日期计算组的大小?我的代码可以生成某个范围内的所有日期...当我单独运行它时可以工作。然后,我想按班级参加注册活动,并在每个日期获得总计的注册人数。 (+1 ==增加类,-1 ==减少类)。
我想我缺少有关联接和分组在SQL中如何工作的基本知识。
http://sqlfiddle.com/#!9/e4835/5/0
样本数据:
CREATE TABLE classes(`id` int, `name` varchar(7));
INSERT INTO classes(`id`, `name`) VALUES
(1, 'math'),
(2, 'english'),
(3, 'sciene');
CREATE TABLE enrollment_changes(
`class_id` int,
`change_date` date,
`change` int);
INSERT INTO enrollment_changes
(`class_id`, `change_date`, `change`)
VALUES
(1, '2019-01-01', 1),
(1, '2019-01-01', 1),
(1, '2019-01-02', -1),
(3, '2019-01-02', 1),
(1, '2019-01-03', 1),
(2, '2019-01-03', -1)
;
-- This gets me part way there... it produces the product of dates x classes
SELECT
date_range.event_date, c.name
FROM
(SELECT adddate('1970-01-01',t4*10000 + t3*1000 + t2*100 + t1*10 + t0) event_date
FROM
(SELECT 0 t0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t0,
(SELECT 0 t1 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t1,
(SELECT 0 t2 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t2,
(SELECT 0 t3 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t3,
(SELECT 0 t4 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t4
) AS date_range
JOIN
classes c
WHERE
date_range.event_date BETWEEN '2019-01-01' AND '2019-01-03'
;
-- This does not work at all... it reduces the output to a single record.
SELECT
date_range.event_date, c.name, SUM(e.change) AS 'NetEnrollment'
FROM
(SELECT adddate('1970-01-01',t4*10000 + t3*1000 + t2*100 + t1*10 + t0) event_date
FROM
(SELECT 0 t0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t0,
(SELECT 0 t1 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t1,
(SELECT 0 t2 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t2,
(SELECT 0 t3 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t3,
(SELECT 0 t4 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t4
) AS date_range
JOIN
classes c
JOIN
enrollment_changes AS e
ON
e.change_date <= date_range.event_date
AND
e.class_id = c.id
WHERE
date_range.event_date BETWEEN '2019-01-01' AND '2019-01-03'
;
当前结果:
event_date name NetEnrollment
------------------------------------
2019-01-01 math 6
所需结果:
event_date name NetEnrollment
------------------------------------
2019-01-01 math 1
2019-01-01 english 0
2019-01-01 science 0
2019-01-02 math 1
2019-01-02 english 1
2019-01-02 science 0
2019-01-03 math 2
2019-01-03 english 1
2019-01-03 science 1
答案 0 :(得分:0)
您可以使用以下解决方案:
SELECT date_range.event_date, c.name, IFNULL(SUM(e.change), 0) AS 'NetEnrollment'
FROM (
SELECT adddate('1970-01-01',t4*10000 + t3*1000 + t2*100 + t1*10 + t0) event_date
FROM
(SELECT 0 t0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t0,
(SELECT 0 t1 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t1,
(SELECT 0 t2 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t2,
(SELECT 0 t3 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t3,
(SELECT 0 t4 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t4
) AS date_range JOIN classes c
LEFT JOIN enrollment_changes AS e ON e.change_date <= date_range.event_date AND c.id = e.class_id
WHERE date_range.event_date BETWEEN '2019-01-01' AND '2019-01-03'
GROUP BY date_range.event_date, c.name
ORDER BY date_range.event_date, c.name
日历表中的每一天都加入所有课程(使用JOIN
)。注册更改(使用LEFT JOIN
)加入到当天和特定班级。使用IFNULL
可以将NULL
的值替换为0。
每天使用GROUP BY
每天会产生一行。要每天为每一堂课取得一行,您必须GROUP BY
天和班级名称。