Вероятно, есть много способов решить эту проблему, я бы использовал GROUP BY.
Сначала вы должны получить разницу между двумя датами. Поскольку вы не указали, какую СУБД (ORACLE, SQL SERVER, My SQL, et c) вы используете, я приведу примеры для oracle и SQL SERVER.
ORACLE:
SELECT (ticket_resolved_date - ticket_created_date) AS DAYS FROM TABLE_NAME;
SQL SERVER:
SELECT DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) AS DAYS FROM TABLE_NAME;
если вы просто сгруппируете по дням и посчитаете (*), то у вас уже будет счет в день, но вы хотите создать категорию ('1-2', '3-4', et c). Для этого вы можете использовать CASE.
ORACLE:
SELECT CASE
WHEN (ticket_resolved_date - ticket_created_date) = 0 THEN 'A - 0'
WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 1 AND 2 THEN 'B - 1-2'
WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 3 AND 4 THEN 'C - 3-4'
WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 5 AND 6 THEN 'D - 5-6'
WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 7 AND 8 THEN 'E - 7-8'
WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 9 AND 10 THEN 'F - 9-10'
WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 11 AND 12 THEN 'G - 11-12'
WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 13 AND 14 THEN 'H - 13-14'
WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 15 AND 16 THEN 'I - 15-16'
WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 17 AND 18 THEN 'J - 17-18'
WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 19 AND 20 THEN 'K - 19-20'
ELSE 'L - 20+'
END AS DAYS_CATEGORY
,COUNT(*) AS "#TICKETS"
FROM DATES
GROUP BY CASE
WHEN (ticket_resolved_date - ticket_created_date) = 0 THEN 'A - 0'
WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 1 AND 2 THEN 'B - 1-2'
WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 3 AND 4 THEN 'C - 3-4'
WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 5 AND 6 THEN 'D - 5-6'
WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 7 AND 8 THEN 'E - 7-8'
WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 9 AND 10 THEN 'F - 9-10'
WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 11 AND 12 THEN 'G - 11-12'
WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 13 AND 14 THEN 'H - 13-14'
WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 15 AND 16 THEN 'I - 15-16'
WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 17 AND 18 THEN 'J - 17-18'
WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 19 AND 20 THEN 'K - 19-20'
ELSE 'L - 20+'
END
ORDER BY 1;
SQL SERVER:
SELECT CASE
WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) = 0 THEN '0'
WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 1 AND 2 THEN 'A - 1-2'
WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 3 AND 4 THEN 'B - 3-4'
WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 5 AND 6 THEN 'C - 5-6'
WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 7 AND 8 THEN 'D - 7-8'
WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 9 AND 10 THEN 'E - 9-10'
WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 11 AND 12 THEN 'F - 11-12'
WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 13 AND 14 THEN 'G - 13-14'
WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 15 AND 16 THEN 'H - 15-16'
WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 17 AND 18 THEN 'I - 17-18'
WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 19 AND 20 THEN 'J - 19-20'
ELSE 'K - 20+'
END AS DAYS_CATEGORY
,COUNT(*) AS "#TICKETS"
FROM TEST
GROUP BY CASE
WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) = 0 THEN '0'
WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 1 AND 2 THEN 'A - 1-2'
WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 3 AND 4 THEN 'B - 3-4'
WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 5 AND 6 THEN 'C - 5-6'
WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 7 AND 8 THEN 'D - 7-8'
WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 9 AND 10 THEN 'E - 9-10'
WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 11 AND 12 THEN 'F - 11-12'
WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 13 AND 14 THEN 'G - 13-14'
WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 15 AND 16 THEN 'H - 15-16'
WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 17 AND 18 THEN 'I - 17-18'
WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 19 AND 20 THEN 'J - 19-20'
ELSE 'K - 20+'
END
ORDER BY 1;