I'm running a simple query that's returning the number of children turning 2 (birthday last month or next month). The following query returns a single row with count:
SELECT COUNT(*)
FROM table AS t
-- Child turning 2
AND t.dob <= DATE_SUB(NOW(), INTERVAL 23 MONTH)
AND t.dob >= DATE_SUB(NOW(), INTERVAL 25 MONTH)
I'd like to be able to build on this query and return multiple count rows, each for a 2 month period so that I can predict 2nd birthdays moving forward.
I could do something like this:
SELECT COUNT(*)
FROM table AS t
-- Child turning 2
AND t.dob <= DATE_SUB(NOW(), INTERVAL 23 MONTH)
AND t.dob >= DATE_SUB(NOW(), INTERVAL 25 MONTH)
UNION
SELECT COUNT(*)
FROM table AS t
-- Child turning 2
AND t.dob <= DATE_SUB(NOW(), INTERVAL 21 MONTH)
AND t.dob >= DATE_SUB(NOW(), INTERVAL 23 MONTH)
UNION...
But this is stupidly inefficient and very clumsy.
As an output I'd like to see something like this:
count |date range
---------------------------------
327 |2012-03-01 - 2012-04-31
---------------------------------
532 |2012-05-01 - 2012-06-31
I think I need to do something with GROUP BY but am unsure about how to go about this.
Many thanks.
select
case
when t.dob between date_sub(now(), interval 25 month) and date_sub(now(), interval 23 month) then 'between -25 and -23 month'
when t.dob between date_sub(now(), interval 22 month) and date_sub(now(), interval 20 month) then 'between -22 and -20 month'
when t.dob between date_sub(now(), interval 19 month) and date_sub(now(), interval 17 month) then 'between -19 and -17 month'
when t.dob between date_sub(now(), interval 16 month) and date_sub(now(), interval 14 month) then 'between -16 and -14 month'
end as my_ranges,
count(*)
from
t
group by my_ranges
Related
I have this Query
SELECT
DATE_ADD(table1.date, INTERVAL -7 DAY) AS date7,
DATE_ADD(table1.date, INTERVAL -14 DAY) AS date14,
DATE_ADD(table1.date, INTERVAL -21 DAY) AS date21,
table1.hotel AS hotel,
table1.customer AS Customer,
table1.date AS Date,
table1.sell AS Sells,
table2.adr AS ADR,
table2.revpar AS RevPAR,
table3.name_hotel AS Hotel_name,
FROM `table1`
INNER JOIN table3 ON table1.hotel=table3.hotel
INNER JOIN table2
ON table1.hotel=table2.hotel
AND table1.date_occ=table2.date_occ
I would like to get a column "weeks" where Date in between Date and date7 then "last week", Date between date7 and date14 then "-2 week" and Date between in date14 and date21 then "-3week". Because I need to group in "weeks" to calculate sells last 7 days, 14 days and 21 days according current date
Something like this should work.
table1.date BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW()
table1.date BETWEEN DATE_SUB(NOW(), INTERVAL 14 DAY) AND DATE_SUB(NOW(), INTERVAL 7 DAY)
table1.date BETWEEN DATE_SUB(NOW(), INTERVAL 21 DAY) AND DATE_SUB(NOW(), INTERVAL 14 DAY)
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-sub
I don't understand why my SQL query doesn't work. I'm trying to do that :
if date is null then return 0
if date < 15 min then return 1
if date > 15 min and < 30 min then return 2
if date > 30 min and < 60 min then return 3
if date > 60 min and < 180 min then return 4
if date > 180 min then return 5
So here is my SQL query :
SELECT CASE
WHEN start_date IS NULL THEN 0
WHEN start_date < DATE_SUB(NOW(),INTERVAL 15 MINUTE) THEN 1
WHEN start_date BETWEEN DATE_SUB(NOW(), INTERVAL 15 MINUTE) AND DATE_SUB(NOW(), INTERVAL 30 MINUTE) THEN 2
WHEN start_date BETWEEN DATE_SUB(NOW(), INTERVAL 30 MINUTE) AND DATE_SUB(NOW(), INTERVAL 60 MINUTE) THEN 3
WHEN start_date BETWEEN DATE_SUB(NOW(), INTERVAL 60 MINUTE) AND DATE_SUB(NOW(), INTERVAL 180 MINUTE) THEN 4
ELSE 5
END as remaining_time
FROM table
But I don't understand why when I got only the cases 0, 1 and 5. remaining_time is never in the. cases 2, 3 ,4.
Do you know why ?
Such condition will never be met, because the lower bound is greater than the higher bound:
start_date BETWEEN DATE_SUB(NOW(), INTERVAL 15 MINUTE) AND DATE_SUB(NOW(), INTERVAL 30 MINUTE)
You should have the bounds the other way around:
SELECT CASE
WHEN start_date IS NULL THEN 0
WHEN start_date < DATE_SUB(NOW(),INTERVAL 15 MINUTE) THEN 1
WHEN start_date BETWEEN DATE_SUB(NOW(), INTERVAL 30 MINUTE) AND DATE_SUB(NOW(), INTERVAL 15 MINUTE) THEN 2
WHEN start_date BETWEEN DATE_SUB(NOW(), INTERVAL 60 MINUTE) AND DATE_SUB(NOW(), INTERVAL 30 MINUTE) THEN 3
WHEN start_date BETWEEN DATE_SUB(NOW(), INTERVAL 180 MINUTE) AND DATE_SUB(NOW(), INTERVAL 60 MINUTE) THEN 4
ELSE 5
END as remaining_time
FROM table
CASE stops on the first matching condition, so this can be simplified as follows:
SELECT CASE
WHEN start_date is NULL THEN 0
WHEN start_date < NOW() - INTERVAL 15 MINUTE THEN 1
WHEN start_date < NOW() - INTERVAL 30 MINUTE THEN 2
WHEN start_date < NOW() - INTERVAL 60 MINUTE THEN 3
WHEN start_date < NOW() - INTERVAL 180 MINUTE THEN 4
ELSE 5
END as remaining_time
FROM table
my script shows the events of the next 14 days:
SELECT
*
FROM
events
WHERE
eventdate
BETWEEN CURDATE() AND DATE_ADD(NOW(), INTERVAL 14 DAY)
This works fine so far BUT at midnight all "running" (night)events are not displayed anymore.
So the query should output the events from the day before till 5 a.m.
This is my idea but it doesn't work:
SELECT
*
FROM
events
WHERE
eventdate
BETWEEN DATE_SUB(CURDATE(), INTERVAL 5 HOUR)
AND
DATE_ADD(NOW(), INTERVAL 14 DAY)
Is it because 'eventdate' is a DATE-field?
Thanks!
It sounds like you want to subtract 5 hours before doing the comparison:
WHERE date_sub(eventdate, interval 5 hour) BETWEEN CURDATE() AND DATE_ADD(NOW(), INTERVAL 14 DAY)
Alternatively, you can add 5 hours on the other side:
where eventdate between now() and datea_add(now(), interval 14*24 + 5 hour)
This should do the job or not!?
WHERE
eventdate
BETWEEN DATE(DATE_SUB(NOW(), INTERVAL 5 HOUR))
AND
DATE_ADD(NOW(), INTERVAL 14 DAY)
I'm trying to fetch some rows from my table based on some condition as follow:
SELECT * FROM MyTable WHERE Date BETWEEN
CASE dayofweek(curdate())
when 1 then curdate() AND adddate(curdate(), interval 6 day)
when 2 then subdate(curdate(), interval 1 day) AND adddate(curdate(), interval 5 day)
when 3 then subdate(curdate(), interval 2 day) AND adddate(curdate(), interval 4 day)
when 4 then subdate(curdate(), interval 3 day) AND adddate(curdate(), interval 3 day)
when 5 then subdate(curdate(), interval 4 day) AND adddate(curdate(), interval 2 day)
when 6 then subdate(curdate(), interval 5 day) AND adddate(curdate(), interval 1 day)
when 7 then subdate(curdate(), interval 6 day) AND curdate()
END
but for some reason it doesn't work. it gives me a syntax error instead. how should I accomplish something like this?
Try this instead,
SELECT *
FROM MyTable
WHERE 1 =
CASE dayofweek(curdate())
when 1 then Date BETWEEN curdate() AND adddate(curdate(), interval 6 day)
when 2 then Date BETWEEN subdate(curdate(), interval 1 day) AND adddate(curdate(), interval 5 day)
when 3 then Date BETWEEN subdate(curdate(), interval 2 day) AND adddate(curdate(), interval 4 day)
when 4 then Date BETWEEN subdate(curdate(), interval 3 day) AND adddate(curdate(), interval 3 day)
when 5 then Date BETWEEN subdate(curdate(), interval 4 day) AND adddate(curdate(), interval 2 day)
when 6 then Date BETWEEN subdate(curdate(), interval 5 day) AND adddate(curdate(), interval 1 day)
when 7 then Date BETWEEN subdate(curdate(), interval 6 day) AND curdate()
END
The CASE() statement ,in this scenario, will only return two possible values: 1 and 0.
A CASE returns a value, not an expression. You must repeat the CASE statement for each side of the BETWEEN:
SELECT * FROM MyTable
WHERE Date BETWEEN
CASE dayofweek(curdate())
when 1 then curdate()
when 2 then subdate(curdate(), interval 1 day)
... etc
END
AND -- this is the "AND" for the BETWEEN values
CASE dayofweek(curdate())
when 1 then adddate(curdate(), interval 6 day)
when 2 then adddate(curdate(), interval 5 day)
... etc
END
So for getting last 24 hours query I use something like this
SELECT COUNT(*) AS cnt FROM `mytable` WHERE timestamp >= DATE_SUB(NOW(), INTERVAL 1 DAY)
where timestamp is a table field with timestamps.
but how can I get the interval between 2 days ago and yesterday.
So
today is 24 ian. I want a query between 22 ian (00:00am) and 23 ian (00:00am)
WHERE timestamp BETWEEN
DATE_SUB(DATE(NOW()), INTERVAL 2 DAY)
AND DATE_SUB(DATE(NOW()), INTERVAL 1 DAY)
You can also try DATE_ADD with a minus interval ;)
WHERE timestamp BETWEEN
DATE_ADD(DATE(NOW()), INTERVAL -2 DAY)
AND DATE_ADD(DATE(NOW()), INTERVAL -1 DAY)
Use Interval
WHERE `timestamp`
BETWEEN DATE_SUB(NOW(), INTERVAL 2 DAY)
AND DATE_SUB(NOW(), INTERVAL 1 DAY)
If you want a query between 22 Jan (00:00 AM) and 22 Jan (11:59 PM)
where DATE(timestamp) = DATE_SUB(DATE(now()), INTERVAL 2 day);
Example:
timestamp = 2020-02-24 12:07:19 and Date(timestamp) is 2020-02-24 and now() output is current date with time when we use DATE(now()) then output is Date only,
DATE_SUB(DATE(now()), INTERVAL 2 day)
is will be 2 days ago.
Try BETWEEN::
SELECT
COUNT(*) AS cnt
FROM `mytable`
WHERE timestamp BETWEEN DATE_SUB(NOW(), INTERVAL 2 DAY) and DATE_SUB(NOW(), INTERVAL 1 DAY)