SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE TABLE TaskSetting ( id int PRIMARY KEY, active bool NULL, task_type varchar NULL ); CREATE TABLE Task ( id int PRIMARY KEY, task_setting_id int NULL, planned_date date NULL, status varchar NULL ); INSERT INTO TaskSetting(id, active, task_type) VALUES (1, true, 'EXTERNAL'); INSERT INTO TaskSetting(id, active, task_type) VALUES (2, false, 'INTERNAL'); INSERT INTO TaskSetting(id, active, task_type) VALUES (3, true, 'INTERNAL'); INSERT INTO TaskSetting(id, active, task_type) VALUES (4, true, 'INTERNAL'); INSERT INTO TaskSetting(id, active, task_type) VALUES (5, true, 'INTERNAL'); INSERT INTO Task(id, task_setting_id, planned_date, status) VALUES(11, 1, '2024-10-17', 'EXECUTED'); INSERT INTO Task(id, task_setting_id, planned_date, status) VALUES(22, 2, '2024-10-16', 'FAILED'); INSERT INTO Task(id, task_setting_id, planned_date, status) VALUES(33, 3, '2024-10-16', 'EXECUTED'); INSERT INTO Task(id, task_setting_id, planned_date, status) VALUES(44, 3, '2024-10-17', 'NEW'); INSERT INTO Task(id, task_setting_id, planned_date, status) VALUES(55, 5, '2024-10-14', 'EXECUTED'); INSERT INTO Task(id, task_setting_id, planned_date, status) VALUES(66, 5, '2024-10-15', 'FAILED'); INSERT INTO Task(id, task_setting_id, planned_date, status) VALUES(77, 5, '2024-10-16', 'EXECUTED'); -- Задача -- Найти количество задач в статусе 'EXECUTED' по каждой TaskSetting (вывести setting_id и количество) SELECT task_setting_id, count(id) FROM Task GROUP BY task_setting_id HAVING status='EXECUTED'; -- Ожидаемый результат: -- (1, 1), (2, 0), (3, 1), (4, 0), (5, 2)

Stuck with a problem? Got Error? Ask ChatGPT!

Copy Clear