我假設你想知道的東西比如在開始和結束之間的每個日期有多少個房間被填滿。這裏的「訣竅」是,從開始/結束之間的很長一段時間將重複一天或一週,並且/或者一週的結束日可能小於一週的開始日。所以,我有:
- 產生100000個日期(1每行)的列表
- 加入你的表的開始/結束
- 轉換的各參加行的週數日之間的日期算
- 左接合到清單1至7,並計數步驟中的行3
注意:如果END_DATE是「簽出日期」,那麼它可能有必要扣除1天從每個記錄要補償(這不是在下面做)。
這種方法可用於審查在這裏SQL Fiddle
的MySQL 5.6架構設置:
CREATE TABLE Table1
(`id` int, `start_date` datetime, `end_date` datetime)
;
INSERT INTO Table1
(`id`, `start_date`, `end_date`)
VALUES
(1, '2017-09-21 00:00:00', '2017-10-07 00:00:00'), ## added this row
(1, '2017-10-01 00:00:00', '2017-10-07 00:00:00'),
(2, '2017-10-04 00:00:00', '2017-10-07 00:00:00'),
(3, '2017-10-06 00:00:00', '2017-10-08 00:00:00')
;
查詢:
set @commence := str_to_date('2000-01-01','%Y-%m-%d')
select
w.dy
, count(t.wdy)
from (
select 1 dy union all select 2 dy union all select 3 dy union all
select 4 dy union all select 5 dy union all select 6 dy union all select 7 dy
) w
left join (
select DAYOFWEEK(cal.dy) wdy
from (
select adddate(@commence ,t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) dy
from ( select 0 i union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t0
cross join (select 0 i union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t1
cross join (select 0 i union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t2
cross join (select 0 i union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t3
cross join (select 0 i union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t4
) cal
INNER JOIN Table1 t on cal.dy between t.start_date and t.end_date
) t on w.dy = t.wdy
group by
w.dy
Results:
| dy | count(t.wdy) |
|----|--------------|
| 1 | 4 |
| 2 | 3 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
| 6 | 6 |
| 7 | 6 |
另見:How to get list of dates between two dates in mysql select query那裏接受的答案是一組交叉的基礎上加入產生從提名日起10萬周的日期。然而,我修改了語法(顯式交叉連接語法),一個參數作爲起點,並使用union all
來提高效率。
使用日曆表(包含您需要的所有日期)並使用BETWEEN條件將其加入到表中。 –
不幸的是,10月份是在星期天開始的。 – Strawberry