表: Tasks+-------------+------+| Column Name | Type |+-------------+------+| task_id | int || assignee_id | int || submit_date | date |+-------------+------+task_id 是此表的主键。此表中的每一行都包含任务 ID、委托人 ID 和提交日期。编写一个 SQL 来查询:在周末 (周六,周日) 提交的任务的数量weekend_cnt,以及工作日内提交的任务数 working_cnt。按 任意顺序 返回结果表。查询结果格式如以下示例所示。示例 1:输入:Tasks 表:+---------+-------------+-------------+| task_id | assignee_id | submit_date |+---------+-------------+-------------+| 1 | 1 | 2022-06-13 || 2 | 6 | 2022-06-14 || 3 | 6 | 2022-06-15 || 4 | 3 | 2022-06-18 || 5 | 5 | 2022-06-19 || 6 | 7 | 2022-06-19 |+---------+-------------+-------------+输出:+-------------+-------------+| weekend_cnt | working_cnt |+-------------+-------------+| 3 | 3 |+-------------+-------------+解释:Task 1 是在周一提交的。Task 2 是在周二提交的。Task 3 是在周三提交的。Task 4 是在周六提交的。Task 5 是在周日提交的。Task 6 是在周日提交的。3 个任务是在周末提交的。3 个任务是在工作日提交的。来源:力扣(LeetCode)链接:https://leetcode.cn/problems/tasks-count-in-the-weekend
#测试数据Create table If Not Exists Tasks (task_id int, assignee_id int, submit_date date);insert into Tasks (task_id, assignee_id, submit_date) values ('1', '1', '2022-06-13');insert into Tasks (task_id, assignee_id, submit_date) values ('2', '6', '2022-06-14');insert into Tasks (task_id, assignee_id, submit_date) values ('3', '6', '2022-06-15');insert into Tasks (task_id, assignee_id, submit_date) values ('4', '3', '2022-06-18');insert into Tasks (task_id, assignee_id, submit_date) values ('5', '5', '2022-06-19');insert into Tasks (task_id, assignee_id, submit_date) values ('6', '7', '2022-06-19');
selectsum(case when weekday(submit_date) in (5,6) then 1 else 0 end) weekend_cnt,sum(case when weekday(submit_date) not in (5,6) then 1 else 0 end) working_cntfrom Tasks;

文章转载自跟强哥学SQL,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




