Hi, I have a table with devices, their buyed country and their buyed date. I want to retrieve : - the total number of devices by country - and the number of devices buyed since 1 month for each country.
I use this request to make this :
SELECT countryCode, COUNT(*) AS sinceBeginning, (
SELECT COUNT(*)
FROM mytable
WHERE countryCode = table1.countryCode
AND buyedDate >= DATE_SUB( CURDATE( ) , INTERVAL 1 MONTH )
) AS sinceOneMonth
FROM mytable AS table1
GROUP BY countryCode
ORDER BY countryCode ASC";
But the inside count for the column "sinceOneMonth" of each row is very expensive for performance. Is there a way to make this request better? Thanks.