(
SELECT
COALESCE(chiusa.data, errata.data) AS data,
COALESCE(chiusi,0) AS chiusi,
COALESCE(errati,0) AS errati
FROM
(
SELECT data, COUNT(*) AS chiusi
FROM utenti_ebay WHERE stato = 'Chiuso'
AND data >= '$data_inizio' AND data <= '$data_fine'
GROUP BY data ORDER BY data
) AS chiusa
LEFT OUTER JOIN
(
SELECT data, COUNT(*) AS errati
FROM utenti_ebay WHERE stato = 'Numero errato'
GROUP BY data ORDER BY data
) AS errata
ON chiusa.data = errata.data
)
UNION ALL
(
SELECT
COALESCE(chiusa.data,errata.data) AS data,
COALESCE(chiusi,0) AS chiusi,
COALESCE(errati,0) AS errati
FROM
(
SELECT data, COUNT(*) AS chiusi
FROM utenti_ebay WHERE stato = 'Chiuso'
GROUP BY data ORDER BY data
) AS chiusa
RIGHT OUTER JOIN
(
SELECT data, COUNT(*) AS errati
FROM utenti_ebay WHERE stato = 'Numero errato'
AND data >= '$data_inizio' AND data <= '$data_fine'
GROUP BY data ORDER BY data
) AS errata
ON chiusa.data = errata.data
WHERE chiusa.data IS NULL
)