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
Related
For example my datetime is 2019/29/08 16:47
My table looks like this
ID stuff timestamp
1 ... 2019/29/08 16:47
2 ... 2019/29/08 16:45
3 ... ...
50 ... 2019/29/08 15:47
... ... ...
100 ... 2019/29/08 14:47
How do I select only the rows, that are 1h, 2h, 3h, .. ,24h before now?
to get something like
ID stuff timestamp
1 ... 2019/29/08 15:47
2 ... 2019/29/08 14:47
3 ... 2019/29/08 13:47
.. ... ...
24 ... 2019/28/08 16:47
SELECT *
FROM YourTable
WHERE timestamp IN ( NOW() - INTERVAL 1 HOUR,
NOW() - INTERVAL 2 HOUR,
NOW() - INTERVAL 3 HOUR,
NOW() - INTERVAL 4 HOUR,
NOW() - INTERVAL 5 HOUR,
NOW() - INTERVAL 6 HOUR,
NOW() - INTERVAL 7 HOUR,
NOW() - INTERVAL 8 HOUR,
NOW() - INTERVAL 9 HOUR,
NOW() - INTERVAL 10 HOUR,
NOW() - INTERVAL 11 HOUR,
NOW() - INTERVAL 12 HOUR,
NOW() - INTERVAL 13 HOUR,
NOW() - INTERVAL 14 HOUR,
NOW() - INTERVAL 15 HOUR,
NOW() - INTERVAL 16 HOUR,
NOW() - INTERVAL 17 HOUR,
NOW() - INTERVAL 18 HOUR,
NOW() - INTERVAL 19 HOUR,
NOW() - INTERVAL 20 HOUR,
NOW() - INTERVAL 21 HOUR,
NOW() - INTERVAL 22 HOUR,
NOW() - INTERVAL 23 HOUR,
NOW() - INTERVAL 24 HOUR)
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
I have a requirement to compare the last 24h with the same period a year ago.
If I do:
event_date < now() - INTERVAL 365 DAY and event_date > now() - INTERVAL 366 DAY
it does not take into consideration leap years.
The where clause you would need to create the condition for the records a year ago would be:
WHERE event_date >= DATE_SUB(DATE_SUB(NOW(), INTERVAL 1 YEAR), INTERVAL 24 HOUR)
AND event_date <= DATE_SUB(NOW(), INTERVAL 1 YEAR)
By doing it this way you will not have to worry about the leap years.
To get the data for the current past 24 hours you would use:
WHERE event_date >= DATE_SUB(NOW(), INTERVAL 24 HOUR)
AND event_date <= NOW()
try
event_date < now() - INTERVAL 1 YEAR
AND event_date > now() - INTERVAL 1 YEAR - INTERVAL 1 DAY
or try
event_date BETWEEN (now() - INTERVAL 1 YEAR - INTERVAL 1 DAY)
AND (now() - INTERVAL 1 YEAR)
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)
How can select the last records added in 5 seconds, 5 minutes, 5 hours, 5 days, 5 weeks, 5 months, 5 years in one query?
Is it possible to be selected in one query?
sample records include
name | added_timestamp
v | last_five_minutes
k | last_hour
l | last_hour
the timestamps can be pseudo for the actual time
Although you haven't stated it, I suspect you want totals for the various time periods:
SELECT
SUM(date_added > NOW() - INTERVAL 5 SECOND) as total_in_last_5_seconds,
SUM(date_added > NOW() - INTERVAL 5 MINUTE) as total_in_last_5_minutes,
SUM(date_added > NOW() - INTERVAL 5 HOUR) as total_in_last_5_hours,
SUM(date_added > NOW() - INTERVAL 5 DAY) as total_in_last_5_days,
SUM(date_added > NOW() - INTERVAL 5 WEEK) as total_in_last_5_weeks,
SUM(date_added > NOW() - INTERVAL 5 MONTH) as total_in_last_5_months,
SUM(date_added > NOW() - INTERVAL 5 YEAR) as total_in_last_5_years
from records;
Edited: Added alternative prompted by comment
If you want the actual records, this will categorize them:
SELECT
*,
case
when date_added > NOW() - INTERVAL 5 SECOND then 'last_5_seconds'
when date_added > NOW() - INTERVAL 5 MINUTE then 'last_5_minutes'
when date_added > NOW() - INTERVAL 5 HOUR then 'last_5_hours'
when date_added > NOW() - INTERVAL 5 DAY then 'last_5_days'
when date_added > NOW() - INTERVAL 5 WEEK then 'last_5_weeks'
when date_added > NOW() - INTERVAL 5 MONTH then 'last_5_months'
when date_added > NOW() - INTERVAL 5 YEAR then 'last_5_years'
else 'ancient'
end as time_category
from records;
SELECT
*, /* now just don't be lazy but specify whatever columns you want to pull */
(Timestamp >= NOW() - INTERVAL 5 SECOND) AS AddedWithinSeconds,
(Timestamp >= NOW() - INTERVAL 5 MINUTE) AS AddedWithinMinutes,
(Timestamp >= NOW() - INTERVAL 5 HOUR) AS AddedWithinHours,
(Timestamp >= NOW() - INTERVAL 5 DAY) AS AddedWithinDays,
(Timestamp >= NOW() - INTERVAL 5 WEEK) AS AddedWithinWeeks,
(Timestamp >= NOW() - INTERVAL 5 MONTH) AS AddedWithinMonths
FROM atable
WHERE Timestamp >= NOW() - INTERVAL 5 YEAR
ORDER BY
AddedWithinSeconds DESC,
AddedWithinMinutes DESC,
AddedWithinHours DESC,
AddedWithinDays DESC,
AddedWithinWeeks DESC,
AddedWithinMonths DESC
Get the last records added in 5 years:
SELECT * FROM records WHERE date_added > NOW() - INTERVAL 5 YEAR
Then extract the records added in 5 seconds, 5 minutes, 5 hours, 5 days, 5 weeks, 5 months from the results.