Current time but for yesterday - mysql

Trying to compare today's current revenue total to yesterdays revenue total where yesterday revenue total is only calculated for the same hours that were calculated for today.
Ex:-
Today: 12 am to 7 pm
Yesterday: 12 am to 7 pm
I want to make sure that yesterday data is always in sync with the current time of today.
Here's what I've tried for pulling yesterday data.
Table Columns:-
date : date ex. 2019-07-08
hour : hour ex. 2019-07-08 23:00:00 (in hour increments)
revenue : revenue
SELECT
sum(revenue) yesterday_revenue
from hour
where hour <= CURRENT_TIME - INTERVAL 24 hour and date >= CURRENT_DATE - INTERVAL 1 day
group by 1

I think this is what you want:
select sum(revenue) as yesterday_revenue
from hour
where hour <= curtime()
date = curdate() - interval 1 day

You can try this below query to get the record as you want.
SELECT
sum(revenue) as yesterday_revenue
from hour
where hour <= DATE_SUB(NOW(), INTERVAL 24 HOUR) and date >= (curdate() - interval 1 day)
group by 1

Related

MySQL select complete last month

How to select all data from last month (or 30 days)?
I already found some answers, and mostly gives this solution
SELECT *
FROM gigs
WHERE date > DATE_SUB(CURDATE(), INTERVAL 3 MONTH)
ORDER BY date DESC
But this gives me also the dates from the future
I am only interested in the days from last month or 30 days (not next month and beyond)
Is this what you want?
WHERE date > DATE_SUB(CURDATE(), INTERVAL 1 MONTH) AND date <= CURRENT_DATE
I added a condition so the query filters on date not greater than today. I also modified your code so the date range starts one month ago (you had 3 months).
try this code
SELECT * FROM gigs
WHERE date BETWEEN CURDATE() - INTERVAL 30 DAY AND CURDATE()
ORDER BY date DESC
You are asking for two separate things.
The last 30 days is easy.
date between now() - interval 30 day and now()
Data this month is like this:
date between (last_day(Now() - INTERVAL 1 MONTH) + INTERVAL 1 DAY) and last_day(Now())
Data a few months ago is like this:
date between (last_day(Now() - INTERVAL 4 MONTH) + INTERVAL 1 DAY)
and
(last_day(Now() - INTERVAL 3 MONTH) + INTERVAL 1 DAY)

how to calculate next 30 days from current date (MySQL)?

i want to get next 30 days record from current date.
any suggestion or tip will be appreciated.
SELECT end_date FROM master_data
WHERE end_date BETWEEN DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY) AND CURRENT_DATE();
You need DATE_ADD not DATE_SUB, because the next 30 days will be between current date and current date + 30:
SELECT end_date
FROM master_data
WHERE end_date BETWEEN CURRENT_DATE()
AND DATE_ADD(CURRENT_DATE(), INTERVAL 30 DAY) ;
Demo

mysql query how to show each day's total cash sale for the current week

I have the following mysql query which shows the each day's total cash sale for the current week.
SELECT
sum(Price) as totalprice,
WEEKDAY(CreatedOn) as dayno,
DATE(CreatedOn) as CreatedOn,
AgentID
FROM records
WHERE CreatedOn BETWEEN (CURDATE()-WEEKDAY(CURDATE())) AND CURDATE()
GROUP BY DATE(CreatedOn)
When I run the query it looks like this:
There are records on 30th November(today's date). So,
day 0 (Monday) no cash sale
day 1 (Tuesday) $5049
day 2 (Wednsday) $99
Nothing is displayed for day 3 (Thursday/today). I cannot figure out the reason there are definitely record in the database but cannot get them to be displayed. I would appreciate any help.
CURDATE() is today's date but at 00:00:00+0000000
"push up" the higher date by 1 day and avoid using between for date/time ranges:
WHERE CreatedOn >= date_sub(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY)
AND CreatedOn < date_add(CURDATE(), INTERVAL 1 DAY)
select date_sub(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY)
, date_add(CURDATE(), INTERVAL 1 DAY)
The condition in the query currently specifies on or before midnight today, so any rows for today after midnight are going to be excluded.
I think you are intending to specify CreatedOn before midnight of the following day.
I also suggest you don't subtract an integer value from a date/datetime, and instead use the INTERVAL syntax
WHERE CreatedOn >= CURDATE() - INTERVAL WEEKDAY(CURDATE()) DAY
AND CreatedOn < CURDATE() + INTERVAL 1 DAY
To test those expressions before we include them in a query, we can run a SELECT statement:
SELECT CURDATE() - INTERVAL WEEKDAY(CURDATE()) DAY
, CURDATE() + INTERVAL 1 DAY
and verify that those expressions are returning what we expect, the values we want to use. For testing, we can replace CURDATE() with a date value to test the return for days other than today.

SELECT date BETWEEN dates with interval

I have a date column in my database. I use SELECT COUNT to calculate the rows between today and 15 days ago:
SELECT count(date) as date
FROM `inv`
WHERE user_id='2'
AND date BETWEEN CURDATE() - INTERVAL 15 DAY
AND CURDATE()
This SQL statement is working. But now I want use SELECT COUNT to calculate the rows between today(-15 days) and 30 days ago. But I am getting an error when I try the following statement:
SELECT count(date) as date
FROM `inv`
WHERE user_id='2'
AND date BETWEEN date(CURDATE(),INTERVAL -15 day)
AND date(CURDATE(),INTERVAL -30 day)
Also I want to know how I can SELECT the rows where the date is more than 30 days ago. Can someone help me with this?
You can use the below to get rows between 15 to 30 days old.
SELECT count(date) as date
FROM `inv`
WHERE user_id=2
AND date BETWEEN CURDATE() - INTERVAL 30 DAY
AND CURDATE() - INTERVAL 15 DAY
Similarly you can use below to get rows that are older than 30 days.
SELECT count(date) as date
FROM `inv`
WHERE user_id=2
AND date < CURDATE() - INTERVAL 30 DAY
Try This
SELECT * FROM "table name" WHERE "user_id=2"
BETWEEN CURDATE() - INTERVAL 30 DAY
AND CURDATE() - INTERVAL 15 DAY

how to date1 and 15 of each month on mysql

I have a table named request that user submit their requests, i will like to get info of all request from first 15 days of the current month and after the 15 days are done the rest of the same month and so an for every month when i'm on the current month.
SELECT *
FROM request
WHERE req_date >= curdate() - INTERVAL DAYOFWEEK(curdate()) + 15 DAY
AND req_date < curdate() - INTERVAL DAYOFWEEK(curdate())- 1 DAY;
this is why I found for pass weeks but is based on current date and I need it to be based on the 1 of the month and 15 of the same month so I can know what happened in the month divided in 2 periods
this is for the first 15
SELECT * FROM request
WHERE
req_date between CAST(DATE_FORMAT(NOW() ,'%Y-%m-01') as DATE)
and CAST(DATE_FORMAT(NOW() ,'%Y-%m-15') as DATE)
;
and this for the second half month
SELECT * FROM request
WHERE
req_date between CAST(DATE_FORMAT(NOW() ,'%Y-%m-16') as DATE)
and LAST_DAY(DATE_ADD(NOW(), INTERVAL 1 MONTH))
;