Используйте рекурсив CTE
, который возвращает все даты 2020
, перекрестно присоедините его к вашему запросу и сгруппируйте по каждой дате:
with recursive days as (
select date('2020-01-01') date
union all
select date(date, '+1 day')
from days
where strftime('%Y', date, '+1 day') = '2020'
)
SELECT days.date, COALESCE(SUM(CAST(pc_loading.value AS INT)), 0)
FROM days
CROSS JOIN ticket
JOIN ticket_custom start_date ON (ticket.id = start_date.ticket AND start_date.name = 'planned_start_date')
JOIN ticket_custom finish_date ON (ticket.id = finish_date.ticket AND finish_date.name = 'planned_finish_date')
JOIN ticket_custom pc_loading ON (ticket.id = pc_loading.ticket AND pc_loading.name = 'percentage_loading')
WHERE UPPER(ticket.owner)='#########' AND ticket.type = 'Internal Resource'
AND DATE(substr(start_date.value, 7, 4) || '-' || substr(start_date.value, 4, 2) || '-' || substr(start_date.value, 1, 2)) <= days.date
AND DATE(substr(finish_date.value, 7, 4) || '-' || substr(finish_date.value, 4, 2) || '-' || substr(finish_date.value, 1, 2)) >= days.date
AND status NOT IN ("closed", "suspended")
GROUP BY days.date
Кроме того, вы найдете значительное улучшение производительности, если вы сохранили даты в сопоставимом формате, например YYYY-MM-DD
, поскольку в предложении WHERE
не было бы необходимости во всех этих перестановках / объединениях. Альтернатива без использования CTE:
SELECT days.date, COALESCE(SUM(CAST(pc_loading.value AS INT)), 0)
FROM (
SELECT '2020-01-01' date UNION ALL SELECT '2020-01-02' UNION ALL
SELECT '2020-01-03' UNION ALL SELECT '2020-01-04' UNION ALL
............................................................
) days
CROSS JOIN ticket
JOIN ticket_custom start_date ON (ticket.id = start_date.ticket AND start_date.name = 'planned_start_date')
JOIN ticket_custom finish_date ON (ticket.id = finish_date.ticket AND finish_date.name = 'planned_finish_date')
JOIN ticket_custom pc_loading ON (ticket.id = pc_loading.ticket AND pc_loading.name = 'percentage_loading')
WHERE UPPER(ticket.owner)='#########' AND ticket.type = 'Internal Resource'
AND DATE(substr(start_date.value, 7, 4) || '-' || substr(start_date.value, 4, 2) || '-' || substr(start_date.value, 1, 2)) <= days.date
AND DATE(substr(finish_date.value, 7, 4) || '-' || substr(finish_date.value, 4, 2) || '-' || substr(finish_date.value, 1, 2)) >= days.date
AND status NOT IN ("closed", "suspended")
GROUP BY days.date