[英]counting rows only if on both days of a weekend
I have a table like this: 我有一张这样的桌子:
id|shift| date|
1| s1|2017-08-12|
2| s2|2017-08-14|
3| s3|2017-08-20|
4| s2|2017-08-26|
5| s3|2017-08-27|
6| s1|2017-08-28|
I'd like to count the number of times any shifts are worked on both days of a weekend (Saturday AND Sunday of the same weekend) and only on both days. 我想计算在周末的两天(同一周末的星期六和星期日)进行的任何轮班次数。
In the example above, the only full weekend worked is 8/26-8/27 so the answer should be 1. 在上面的示例中,唯一的完整周末是8 / 26-8 / 27,因此答案应为1。
Any help is much appreciated. 任何帮助深表感谢。
Join the table with itself. 自己加入桌子。 (Join sundays with the the previous day).
(加入前一天的星期日)。
select count(*)
from yourTable sun -- sunday
join yourTable sat -- saturday
on sat.shift = sun.shift
and sat.date = sun.date - interval 1 day
where dayofweek(sun.date) = 1 -- sunday
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.