I need help with some dates in mysql
I have this query that brings me the count of the messages of the corresponding dates of every message from the last 30 days.
SELECT DATE_FORMAT(time, '%m/%d/%Y') AS Dates, count(*) as count
FROM ma_messages
WHERE time BETWEEN NOW() - INTERVAL 30 DAY AND NOW()
AND usersid_send = 110
Group by Dates
ORDER BY Dates ASC
query image
But I need that also bring me the other dates of the last 30 days with a value in the count of 0, for example that in the query also bring me 05/26/2021 date and the dates before 05/25/2021 and after 05/28/2021.
I don't know if this is possible, but I will apreciate any help.
Thanks.
try this
SELECT
DATE_FORMAT(time, '%m/%d/%Y') AS Dates,
count(if(usersid_send='110',true,null)) as count
FROM ma_messages
WHERE time BETWEEN NOW() - INTERVAL 30 DAY AND NOW()
Group by Dates
ORDER BY Dates ASC
I have a table with dates .. with all the dates of the year – Alexis Murillo
SELECT d.`date`, COUNT(m.`time`) AS `count`
FROM dates_table d
LEFT JOIN ma_messages m ON d.`date` = DATE(m.`time`)
WHERE d.`date` BETWEEN NOW() - INTERVAL 30 DAY AND NOW()
AND m.usersid_send = 110
GROUP BY d.`date`
ORDER BY d.`date` ASC
Using your table "with all the dates of the year":
WITH
message_counts AS ( SELECT DATE_FORMAT("time", '%m/%d/%Y') AS "day",
COUNT(*) AS "count"
FROM ma_messages
WHERE "time" BETWEEN NOW() - INTERVAL 30 DAY AND NOW()
AND
usersid_send = 110
GROUP BY Dates)
SELECT all_dates."day", COALESCE(message_counts."count", 0)
FROM all_dates
LEFT JOIN message_counts
ON all_dates."day" = message_counts."day"
WHERE all_dates."day" BETWEEN BETWEEN NOW() - INTERVAL 30 DAY AND NOW()
ORDER BY all_dates."day" ASC
Related
I have the following table called vacations, where the employee id is displayed along with the start and end date of their vacations:
employee
start
end
1001
26/10/21
22/11/21
What I am looking for is to visualize the number of vacation days that each employee had, but separating them by month and without non-working days (Saturdays and Sundays).
For example, if you wanted to view the vacations for employee 1001, the following result should be displayed:
days
month
4
10
16
11
I have the following query that I have worked with:
SELECT id_employee,
EXTRACT(YEAR_MONTH FROM t.Date) as YearMonth,
COUNT(1) as Days
FROM (SELECT v.id_employee,
DATE_ADD(v.start, interval s.seq - 1 DAY) AS Date
FROM vacations v
CROSS JOIN seq_1_to_100 s
WHERE DATE_ADD(v.start, interval s.seq - 1 DAY) <= v.end
ORDER BY v.id_employee, , v.start, s.seq
) t
GROUP BY id_employee,
EXTRACT(YEAR_MONTH FROM t.Date)
With this query I separate the days between a range of two dates with their respective month, but how could I adapt it to stop considering Saturdays and Sundays? I'm working with MySQL 5.7 in phpMyAdmin
instead of count sum the compaarison of weekday function, which give what day it is .
But you should always save fates n a valid mysql manner 2021-10-28
SELECT id_employee,
EXTRACT(YEAR_MONTH FROM t.Date) as YearMonth,
SUM(WEEKDAY(`Date`) < 5) as Days
FROM (SELECT v.id_employee,
DATE_ADD(v.start, interval s.seq - 1 DAY) AS Date
FROM vacations v
CROSS JOIN seq_1_to_100 s
WHERE DATE_ADD(v.start, interval s.seq - 1 DAY) <= v.end
ORDER BY v.id_employee, v.start, s.seq
) t
GROUP BY id_employee,
EXTRACT(YEAR_MONTH FROM t.Date)
I want to group all last 30 days purchases in a store by each day and return last 30 days array of following data for example
2017/04/01
purchases: 30
total: 900.01
2017/04/02
purchases: 30
total: 900.01
and so on. so far I have no idea how to make this kind of query and came up with following idea
SELECT COALESCE(SUM(purchases.price)/1000,0) AS all_purchases,
min(purchases.time) AS start_interval, max(purchases.time) AS end_interval
FROM purchases
WHERE purchases.time::date >= DATE_SUB(NOW(), INTERVAL 30 DAY) AND WHERE purchases.time::date <= DATE_SUB(NOW())
ORDER BY start_interval DESC
but thats not how it works
You should use group by eg:
SELECT date(purchases.time) as my_date , count(*) as my_count,
COALESCE(SUM(purchases.price)/1000,0) AS all_purchases
FROM purchases
WHERE date(purchases.time) >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)
AND date(purchases.time) <= CURDATE()
GROUP BY date(purchases.time)
ORDER BY my_date DESC
(and you should use where only one time .. not where ..and where but where ... and .. )
I have a table name transactions where data rows are stored with a date
Like
`trans_id` `amount` `tdate`
I want to filter that data like last 30 days, last 31st 60 days, last 61-90 days calculate the overall amount also
My queries are
For last 30 days
SELECT SUM(amount) AS amt FROM transactions GROUP BY DATE(tdate) ORDER BY DATE(tdate) DESC LIMIT 30
Working fine and show SUM of amount (last 30days)
But for last 31-60 days not working
SELECT SUM(amount) AS amt FROM transactions GROUP BY DATE(tdate) ORDER BY DATE(tdate) DESC LIMIT 60,31
How to solve it ? I do want to include only 31 to 60 days amount only
Use the following: (It returns data between today and last 30 days)
SELECT DATE_FORMAT(DateCol, '%m/%d/%Y')
FROM Table
WHERE DateCol BETWEEN CURDATE() - INTERVAL
30 DAY AND CURDATE()
Or more precisely, this will do the trick:
DateCol BETWEEN (NOW() - INTERVAL 60 DAY)
AND (NOW() - INTERVAL 30 DAY)
CURDATE() works for only date portion. If you have DateTime column, then NOW() will do.
I have a number of posts saved into a InnoDB table on MySQL. The table has the columns "id", "date", "user", "content". I wanted to make some statistic graphs, so I ended up using the following query to get the amount of posts per hour of yesterday:
SELECT HOUR(FROM_UNIXTIME(`date`)) AS `hour`, COUNT(date) from fb_posts
WHERE DATE(FROM_UNIXTIME(`date`)) = CURDATE() - INTERVAL 1 DAY GROUP BY hour
This outputs the following data:
I can edit this query to get any day I want. But what I want now is the AVERAGE of each hour of every day, so that if on Day 1 at 00 hours I have 20 posts and on Day 2 at 00 hours I have 40, I want the output to be "30". I'd like to be able to pick date periods as well if it's possible.
Thanks in advance!
You can use a sub-query to group the data by day/hour, then take the average by hour across the sub-query.
Here's an example to give you the average count by hour for the past 7 days:
select the_hour,avg(the_count)
from
(
select date(from_unixtime(`date`)) as the_day,
hour(from_unixtime(`date`)) as the_hour,
count(*) as the_count
from fb_posts
where `date` >= unix_timestamp(current_date() - interval 7 day)
and created_on < unix_timestamp(current_date())
group by the_day,the_hour
) s
group by the_hour
Aggregate the information by date and hour, and then take the average by hour:
select hour, avg(numposts)
from (SELECT date(`date`) as day, HOUR(FROM_UNIXTIME(`date`)) AS `hour`,
count(*) as numposts
from fb_posts
WHERE DATE(FROM_UNIXTIME(`date`)) between <date1> and <date2>
GROUP BY date(`date`), hour
) d
group by hour
order by 1
By the way, I prefer including the explicit order by, since most databases do not order the results of a group by. Mysql happens to be one database that does.
SELECT
HOUR(FROM_UNIXTIME(`date`)) AS `hour`
, COUNT(`id`) \ COUNT(DISTINCT TO_DAYS(`date`)) AS avgHourlyPostCount
FROM fb_posts
WHERE `date` > '2012-01-01' -- your optional date criteria
GROUP BY hour
This gives you a count of all the posts, divided by the number of days, by hour.
I am doing a query to get the number of builds per day from our database for the last 30 days. But it has become needed to marked days where there were no builds also.
In my WHERE clause I use submittime to determine whether there were builds, how could I modify this to include days that have COUNT(id) == 0 but only in the last 30 days.
Original Query:
SELECT COUNT(id) AS 'Past-Month-Builds',
CONCAT(MONTH(submittime), '-', DAY(submittime)) as 'Month-Day'
FROM builds
WHERE DATE(submittime) >= DATE_SUB(CURDATE(), INTERVAL 30 day)
GROUP BY MONTH(submittime), DAY(submittime);
What I've Tried:
SELECT COUNT(id) AS 'Past-Month-Builds',
CONCAT(MONTH(submittime), '-', DAY(submittime)) as 'Month-Day'
FROM builds
WHERE DATE(submittime) >= DATE_SUB(CURDATE(), INTERVAL 30 day)
OR COUNT(id) = 0
GROUP BY MONTH(submittime), DAY(submittime);
You need a table of dates, then left join to the builds table.
Something like this:
SELECT
COUNT(id) AS 'Past-Month-Builds',
CONCAT(MONTH(DateTable.Date), '-', DAY(DateTable.Date)) as 'Month-Day'
FROM DateTable
LEFT JOIN builds ON DATE(builds.submittime) = DateTable.Date
WHERE DateTable.Date >= DATE_SUB(CURDATE(), INTERVAL 30 day)
GROUP BY MONTH(submittime), DAY(submittime);