Bug fix of query to find days to next free slot
templates/queries-index-mysql.tpl.html, templates/queries-index-sqlite.tpl.html: Rather than driving the search from the eps table itself, this version makes a counter that generates slot numbers from the current show number to the highest show number in the system. The counter is used to interrogate the eps table to find the first empty slot. This sems to be a more reliable approach (but time will tell).
This commit is contained in:
parent
dab80f1772
commit
c8135d811d
@ -1,29 +1,31 @@
|
|||||||
<!--% query_next_available_episode = '
|
<!--% query_next_available_episode = '
|
||||||
WITH next_id AS (
|
WITH RECURSIVE
|
||||||
SELECT id, id + 1 AS id_next, date as last_date
|
min_id AS (
|
||||||
FROM eps
|
SELECT max(id) AS start_id FROM eps WHERE date <= NOW()
|
||||||
WHERE eps.date > NOW()
|
|
||||||
),
|
),
|
||||||
free_slot AS (
|
max_id AS (
|
||||||
|
SELECT max(id) AS end_id FROM eps
|
||||||
|
),
|
||||||
|
cnt(x) AS (
|
||||||
|
SELECT start_id from min_id
|
||||||
|
UNION ALL
|
||||||
|
SELECT x+1 FROM cnt
|
||||||
|
WHERE x+1 <= (SELECT end_id FROM max_id)
|
||||||
|
),
|
||||||
|
first_empty AS (
|
||||||
SELECT
|
SELECT
|
||||||
last_date AS prev_date,
|
cnt.x AS id
|
||||||
CASE
|
FROM cnt
|
||||||
WHEN DAYOFWEEK(last_date) = 6
|
LEFT JOIN eps e ON cnt.x = e.id
|
||||||
THEN 3
|
WHERE e.id IS NULL
|
||||||
ELSE 1
|
|
||||||
END AS date_offset,
|
|
||||||
next_id.id_next AS empty_slot
|
|
||||||
FROM next_id
|
|
||||||
LEFT JOIN eps ON next_id.id_next = eps.id
|
|
||||||
WHERE eps.id IS NULL
|
|
||||||
ORDER BY next_id.id
|
|
||||||
LIMIT 1
|
|
||||||
)
|
)
|
||||||
SELECT
|
SELECT
|
||||||
empty_slot AS next_id,
|
fe.id as next_id,
|
||||||
ADDDATE(prev_date,date_offset) AS next_date,
|
(SELECT DATE(ADDDATE(e2.date,1)) from eps e2 where e2.id = fe.id-1) as next_date,
|
||||||
DATEDIFF(ADDDATE(prev_date,date_offset),NOW()) AS delta_days
|
DATEDIFF((SELECT ADDDATE(e3.date,1) from eps e3 where e3.id = fe.id-1),now()) AS delta_days
|
||||||
FROM free_slot
|
FROM first_empty AS fe
|
||||||
|
LEFT JOIN eps e1 ON fe.id = e1.id
|
||||||
|
LIMIT 1
|
||||||
'
|
'
|
||||||
%-->
|
%-->
|
||||||
<!--% query_latest_episodes = '
|
<!--% query_latest_episodes = '
|
||||||
|
@ -20,8 +20,8 @@
|
|||||||
WHERE e.id IS NULL
|
WHERE e.id IS NULL
|
||||||
)
|
)
|
||||||
SELECT
|
SELECT
|
||||||
fe.id as empty_slot,
|
fe.id as next_id,
|
||||||
(SELECT date(eps.date,\'+1 day\') from eps where id = fe.id-1) as slot_date,
|
(SELECT date(eps.date,\'+1 day\') from eps where id = fe.id-1) as next_date,
|
||||||
printf(\'%i\',abs(julianday((SELECT date(eps.date,\'+1 day\') from eps where id = fe.id-1)) - julianday(\'now\') + 1)) AS delta_days
|
printf(\'%i\',abs(julianday((SELECT date(eps.date,\'+1 day\') from eps where id = fe.id-1)) - julianday(\'now\') + 1)) AS delta_days
|
||||||
FROM first_empty AS fe
|
FROM first_empty AS fe
|
||||||
LEFT JOIN eps e ON fe.id = e.id
|
LEFT JOIN eps e ON fe.id = e.id
|
||||||
|
Reference in New Issue
Block a user