Hi I'm having trouble sorting image data by month, I'd like to be able to tell how many images were created, updated and deleted within a certain month by getting the count and grouping then by the month. I have two tables with image data in. Both tables have 'createdAt', 'updatedAt' and 'deletedAt' columns so what I did was date format the columns of both tables and union them to have all of the data together but I'm having trouble getting the count of the data. Here is what I have so far:
SELECT DATE_FORMAT(`createdAt`, "%M %Y"), DATE_FORMAT(`updatedAt`, "%M %Y"), DATE_FORMAT(`deletedAt`, "%M %Y") FROM harvest_sample_images UNION SELECT DATE_FORMAT(`createdAt`, "%M %Y"), DATE_FORMAT(`updatedAt`, "%M %Y"), DATE_FORMAT(`deletedAt`, "%M %Y") FROM plant_sample_images
Any help would be appreciated.
SELECT DATE_FORMAT(`createdAt`, "%M %Y") DAT, count(*) AS `created`,
(SELECT DATE_FORMAT(`updatedAt`, "%M %Y") DAT, COUNT(*)) AS `updated`,
(SELECT DATE_FORMAT(`deletedAt`, "%M %Y") DAT COUNT(*)) AS `deleted`
FROM (SELECT `createdAt`, `updatedAt`, `deletedAt` FROM harvest_sample_images UNION SELECT `createdAt`, `updatedAt`, `deletedAt` FROM plant_sample_images)
GROUP BY DATE_FORMAT(`created`, "M% %Y")
I have tried this now.
So I might have found another way of sorting the images info into each respective month with the count as well as displaying the month it falls within.
SELECT DATE_FORMAT(`timestamp_date`, "%M %Y") AS `Month`,
(SELECT COUNT(*) FROM (SELECT `createdAt` FROM harvest_sample_images UNION ALL SELECT `createdAt` FROM plant_sample_images) AS `images_created` WHERE DATE_FORMAT(images_created.`createdAt`, "%m %y") = DATE_FORMAT(`timestamp_date`, "%m %y")) AS `Created`,
(SELECT COUNT(*) FROM (SELECT `updatedAt` FROM harvest_sample_images UNION ALL SELECT `updatedAt` FROM plant_sample_images) AS `images_updated` WHERE DATE_FORMAT(images_updated.`updatedAt`, "%m %y") = DATE_FORMAT(`timestamp_date`, "%m %y")) AS `Updated`,
(SELECT COUNT(*) FROM (SELECT `deletedAt` FROM harvest_sample_images UNION ALL SELECT `deletedAt` FROM plant_sample_images) As `images_deleted` WHERE DATE_FORMAT(images_deleted.`deletedAt`, "%m %y") = DATE_FORMAT(`timestamp_date`, "%m %y")) AS `Deleted`
FROM table_dates
So what I'm doing now is , I have created another table with list of dates ranging from the start of the data to the present. I then loop through the data and check if the formatted date of the table_dates if equal to the formatted date of the union of the two images tables date. I the get the count and add it as a column in the new table. Thoughts are appreciated