sammy
sammy

Reputation: 87

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.

Any help is much appreciated.

Upvotes: 0

Views: 33

Answers (1)

Paul Spiegel
Paul Spiegel

Reputation: 31772

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

Upvotes: 1

Related Questions