Mysql retrieve last 30 days data and sum it by days - mysql

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 .. )

Related

Mysql dates of the last 30 days

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

Apply group by and limit offset together in MySQL

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.

MySQL: search orders made since yesterday 3pm till today 4pm

I have table ORDERS where is stored data about orders with their status and the date of order. I would like to search all orders with specified status and which was made yesterday after 3pm untill today 4pm. The query will run in different times (10am, 3pm, 5 pm... regardless).
So on example: if I run the query today (13.05.2014) I would like to get all orders made from 2014-12-05 15:00:00 untill 13-05-2015 16:00:00
The date is stored in format: YYYY-MM-DD HH:MM:SS
What I got is:
select *
from orders
where status = 'new'
and (
(
date_add(created_at, INTERVAL 1 day) = CURRENT_DATE()
and hour(created_at) >= 15
) /*1*/
or (
date(created_at) = CURRENT_DATE()
and hour(created_at) <= 16
) /*2*/
)
And I get only orders made today - like only the 2nd condition was taken into account.
I prefer not to use created >= '2014-05-12 16:00:00' (I will not use this query, someone else will).
When you add an interval of 1 day to the date/time, you still keep the time component. Use date() for the first condition:
where status = 'new' and
((date(date_add(created_at, INTERVAL 1 day)) = CURRENT_DATE() and
hour(created_at) >= 15
) /*1*/ or
(date(created_at) = CURRENT_DATE() and
hour(created_at) <= 16
) /*2*/
)
And alternative method is:
where status = 'new' and
(created_at >= date_add(CURRENT_DATE(), interval 15-24 hour) and
created_at <= date_add(CURRENT_DATE(), interval 16 hour)
)
The advantage of this approach is that all functions are moved to CURRENT_DATE(). This would allow MYSQL to take advantage of an index on created_at.

mysql select dates in 30-day range

This must be simple but I fiddled with it, and didn't get anything I wanted. I have the following code:
SELECT id,title,start_date
FROM events
WHERE start_date > DATE_SUB(NOW(), INTERVAL 1 MONTH)
AND city = '$cityName'
ORDER BY start_date DESC
Now this selects events with dates in this month, but the definition of this month shown in query is different than what I need. I need it to show me events within 30 days and not only this month i.e. august. If I insert an event in august it shows the outcome. If I do insert september, it doesn't even though it is less than 30 days away.
You should change 1 MONTH to 30 DAY:
WHERE start_date > NOW() - INTERVAL 30 DAY
To limit it to 30 days in either direction:
WHERE start_date > NOW() - INTERVAL 30 DAY
AND start_date < NOW() + INTERVAL 30 DAY
How about like this:
...WHERE DATE(start_date) BETWEEN DATE_SUB(NOW(),INTERVAL 30 DAY) and DATE_SUB(NOW(),INTERVAL 1 DAY) AND city...
OR
AND TIMESTAMPDIFF(DAY,YOURDATE,now()) < 30
This gives you a 30 day span
I hope this will help also
SELECT id,title,start_date
FROM events
WHERE city = "$cityName" AND
TIMESTAMPDIFF(DAY,start_date,now()) < 30
ORDER BY start_date DESC

MySql Query: include days that have COUNT(id) == 0 but only in the last 30 days

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);