+---------+---------------------+
| user_id | registration_date |
+---------+---------------------+
| 6988 | 2017-07-24 12:10:29 |
| 6985 | 2017-07-23 12:10:00 |
| 6980 | 2017-07-22 11:10:40 |
| 6979 | 2017-07-21 02:30:00 |
| 6978 | 2017-07-20 08:10:15 |
| 6977 | 2017-07-19 12:10:29 |
| 6976 | 2017-07-18 12:10:00 |
| 6975 | 2017-07-17 05:10:02 |
| 6974 | 2017-07-16 06:10:11 |
| 6951 | 2017-07-15 09:10:50 |
+---------+---------------------+
select registration_date from users WHERE registration_date BETWEEN '2017-07-24 12:10' - INTERVAL 10 DAY AND '2017-07-24 12:10';
I am having this data in my sql table and i am trying to get the data between 2017-07-24 12:10 and INTERVAL 10 DAY AND '2017-07-24 12:10'(excluding seconds).
Now i want to get this | 6988 | 2017-07-24 12:10:29 | ...Means i want to ignore the seconds value from the data that is stored in the db and then get the data.so that the desired data with user_id 6988 will come.
select registration_date from users WHERE registration_date BETWEEN '2017-07-24 12:10' - INTERVAL 10 DAY AND '2017-07-24 12:10';
I am trying this...But not working .
Add the min and max value for seconds. In start add the 00 and for the range end 59 as seconds
select registration_date from users
WHERE registration_date BETWEEN '2017-07-24 12:10:00' - INTERVAL 10 DAY
AND '2017-07-24 12:10:59';
For MySQL, you can use DATE and STR_TO_DATE functions to extract/compare dates, e.g.:
SELECT DATE_FORMAT(registration_date, '%Y-%m-%d %H:%i')
FROM users
WHERE registration_date BETWEEN DATE_ADD(STR_TO_DATE('2017-07-24 12:10', '%Y-%m-%d %H:%i') - INTERVAL 10 DAY)
AND STR_TO_DATE('2017-07-24 12:10', '%Y-%m-%d %H:%i');
Here's the documentation.
You can use DATE_FORMAT to get the parts of the date that you want, and compare this.
Since you want a range of dates, you need to test that separately from the time.
WHERE DATE(registration_date) BETWEEN '2017-07-14' AND '2017-07-24'
AND DATE_FORMAT(registration_date, '%H:%i') = '12:10'
Related
I have a data set representing alarms' state at a given timestamp (every 15 minutes). When the value is 1 the alarm is ON, 0 when OFF. I am trying to count the number of times the alarm has been triggered per hour (non-consecutive 1).
I took a look at Count max number of consecutive occurrences of a value in SQL Server but couldn't manage to adapt the answer.
Basically the data set for one alarm looks like this:
| id | value | registered_at |
| -- | ---------|---------------------|
| 1 | 1 | 2012-07-15 06:00 |
| 2 | 0 | 2012-07-15 06:15 |
| 3 | 1 | 2012-07-15 06:30 |
| 4 | 0 | 2012-07-15 06:45 |
| 5 | 1 | 2012-07-15 07:00 |
| 6 | 1 | 2012-07-15 07:15 |
| 7 | 1 | 2012-07-15 07:30 |
| 8 | 0 | 2012-07-15 07:45 |
| 8 | 0 | 2012-07-15 08:00 |
The results I am looking for is the following
| registered_at | alarm_triggered |
|--------------------|-----------------|
| 2012-07-15 06 | 2 |
| 2012-07-15 07 | 1 |
| 2012-07-15 08 | 0 |
To create groups I use EXTRACT(DAY_HOUR from registered_at).
Can you help me create the query?
(First time poster on SO, any feedback about the form of this post would be greatly appreciated as well)
Use LAG() window function to check the value of value of the previous row and if it is different and the current row is 1 then sum:
SELECT registered_at,
SUM(value * flag) alarm_triggered
FROM (
SELECT value,
DATE_FORMAT(registered_at, '%Y-%m-%d %H') registered_at,
value <> LAG(value, 1, 0) OVER (PARTITION BY DATE_FORMAT(registered_at, '%m-%d-%Y %H') ORDER BY registered_at) flag
FROM tablename
) t
GROUP BY registered_at
See the demo.
Results:
registered_at
alarm_triggered
2012-07-15 06
2
2012-07-15 07
1
2012-07-15 08
0
I assume the registered_at field is datetime so you need to use datetime function.
here is a query for this:
SELECT DATE_FORMAT(registered_at, "%Y-%m-%d %H:00:00") AS registered_at, SUM(VALUE) AS alarm_triggered
FROM ALARMS
GROUP BY DATE_FORMAT(registered_at, "%Y-%m-%d %H:00:00")
and sqlfiddle to see example:
example
If you need only notified days
select count(value), date_format(registered_at, '%m-%d-%Y %H') as c_at
from notifications
where value = 1
group by date_format(registered_at, '%m-%d-%Y %H');
Or all days
select sum(value), date_format(registered_at, '%m-%d-%Y %H') as c_at
from notifications
group by date_format(registered_at, '%m-%d-%Y %H');
Try this!
You can select it like this:
SELECT CONCAT(YEAR(registered_at), '-', MONTH(registered_at), '-', DAYOFMONTH(registered_at), ' ' HOUR(registered_at)), count(*)
FROM alarms
WHERE value = 1
GROUP BY YEAR(registered_at), MONT(registered_at), DAYOFMONTH(registered_at), HOUR(registered_at);
Explanation
First, we find the records whose value is 1, then group them by year, month, day of month and hour and finally we find out their count.
Assuming the following data:
+--------------------------+
| id | created_at |
+--------------------------+
| 1 | 2019-04-23 13:01:00 |
| 2 | 2019-04-23 13:18:00 |
| 3 | 2019-04-30 13:01:00 |
| 4 | 2019-04-30 13:06:00 |
| 5 | 2019-04-30 13:17:00 |
| 6 | 2019-04-30 13:23:00 |
| 7 | 2019-04-30 13:32:00 |
| 8 | 2019-05-04 13:19:00 |
| 9 | 2019-05-04 13:41:00 |
| 10 | 2019-05-04 13:51:00 |
+----+---------------------+
I'd like to fetch entries where created_at was in a 15 minutes window
3 days ago OR every repeated 7 days in the past from NOW()
For example, if i'm running the query at 2019-05-07 13:30:00, I would like to fetch entries #2 (within 14 days ago + 15 minutes), #5 (within 7 days ago + 15 minutes) and #8 (within 3 days ago + 15 minutes).
I can easily write the 3 days condition, but i'm struggling with the repeated 7 days condition.
My query so far :
SELECT id, created_at
FROM user_abstract
WHERE TIMESTAMPDIFF(MINUTE, created_at, NOW()) BETWEEN 3*24*60 AND 3*24*60+15
I created a fiddle with the above dataset and the query so far, where NOW() can be customized for testing: https://www.db-fiddle.com/f/xa4ZEqEcGUDa2N3MLrgvh3/0
Any help would be appreciated.
What about this?
SET #now = '2019-05-07 13:30:00';
SELECT id, created_at
FROM user_abstract
WHERE TIMESTAMPDIFF(MINUTE, created_at, NOW()) BETWEEN 3*24*60 AND 3*24*60+15
or MOD(TIMESTAMPDIFF(MINUTE, created_at, #now),7*24*60) <= 15
The conditions you are trying to apply are distinct so these conditions are must apply, I made changes in your query(6th entry lies in 2nd mentioned range)
SET #now = '2019-05-07 13:30:00';
SELECT id, created_at
FROM user_abstract
WHERE (TIMESTAMPDIFF(MINUTE, created_at, #now) BETWEEN 3*24*60 AND 3*24*60+15) OR
MOD(TIMESTAMPDIFF(MINUTE, created_at, #now),7*24*60) <= 15
Working demo.
I want to get the number of sales per hour in a specific date:
table : invoices
+-------+-----------+-----------+--------------------------+
| id | name | amout | date |
+-------+-----------+-----------+--------------------------+
| 1 | John | 12313 | 2017-05-20 13:50:08 |
| 2 | Mary | 5335 | 2017-05-17 22:21:35 |
| 3 | Jeff | 23 | 2017-05-17 22:32:13 |
| 4 | Bill | 132 | 2017-05-17 23:25:55 |
| 5 | Bob | 853 | 2017-05-17 24:52:37 |
+-------+-----------+-----------+--------------------------+
So, I want to get this output:
9 a.m. to 10 a.m. we generated X invoices.
10 a.m. to 11 a.m. we generated Y invoices.
11 a.m. to 12 a.m. we generated Z invoices.
X,Y and Z it's the number of invoices generated in these interval.
How I can do that? I use MySQL.
Thanks!
Three SQL concepts will help you get an answer to this question.
"Truncating" a datestamp; that is, extracting the hour from it. For example, 2017-05-27 14:37.20 is truncated to 2017-05-27 14:00:00.
Selecting rows from your table with datestamps on a particular day.
Using COUNT(*) and GROUP BY.
You can truncate like this.
SELECT DATE_FORMAT(`date`, '%y-%m-%d %H:00:00') hour_starting
FROM invoices
You can add to that query to select rows for a particular date like this
SELECT DATE_FORMAT(`date`, '%y-%m-%d %H:00:00') hour_starting
FROM invoices
WHERE `date` >= '2017-05-27`
AND `date` < '2017-05-27' + INTERVAL 1 DAY
Notice that this chooses all rows with date values on or after (>=) midnight on 2017-05-27. and before but not including (<) midnight on the next day.
Finally, you can use COUNT like this.
SELECT DATE_FORMAT(`date`, '%y-%m-%d %H:00:00') hour_starting,
COUNT(*) invoice_count
FROM invoices
WHERE `date` >= '2017-05-27`
AND `date` < '2017-05-27' + INTERVAL 1 DAY
GROUP BY DATE_FORMAT(`date`, '%y-%m-%d %H:00:00')
These are my rows in MySql database table:
+------------+----------+-------------+
| theDate | theHour | theUserCode |
+------------+----------+-------------+
| 2015-11-16 | 06:30:00 | XX2111905 |
| 2015-11-16 | 21:30:37 | XX2112111 |
| 2015-11-16 | 22:21:29 | XX2112111 |
| 2015-11-16 | 17:15:18 | XX2142122 |
| 2015-11-16 | 04:22:13 | XX2146905 |
| 2015-11-16 | 15:15:00 | XX2146905 |
| 2015-11-16 | 21:26:00 | XX2148516 |
+------------+----------+-------------+
7 rows in set
I need extract from this table the rows with theHour between 15:00:00 and 03:00:00.
I have tried this Sql query but in output I have the Empty set.
SELECT
theDate,
theHour,
theUserCode
FROM
`tblUserRegistered`
WHERE
theDate BETWEEN DATE_SUB('2015-11-16', INTERVAL 1 DAY)
AND '2015-11-16'
AND theHour BETWEEN '15:00:00'
AND '03:00:00'
ORDER BY
theUserCode,
theDate,
theHour ASC;
Please help me, thank you so much in advance.
I used the HOUR() function in my query below to handle the check by hour. This query will include records later than 3pm (15 hours), or earlier than 3am (03 hours).
SELECT theDate, theHour, theUserCode
FROM tblUserRegistered
WHERE theDate BETWEEN DATE_SUB('2015-11-16', INTERVAL 1 DAY) AND '2015-11-16'
AND (HOUR(theHour) >= 15 OR HOUR(theHour) <= 3)
ORDER BY theUserCode, theDate, theHour ASC
I have a mysql database with which contains data in 5 minute bins. I'd like to create hourly average of the data starting on the half hour.
By using mysql built-in group by:
select date,AVG(AE) from mytable group by date(date),HOUR(date);
would compute average value from say, 01:00 to 02:00. Instead I would like hourly averages to be computed from 00:30 to 01:30, were the value would then be the hourly average at 01:00.
This query fail when a new day starts:
select date, AVG(AE) from mytable group by date(date), HOUR( date ) + FLOOR( MINUTE( date ) / 30 );
+---------------------+------------------+
| date | AVG(AE) |
+---------------------+------------------+
| 1997-01-01 22:30:00 | 23 |
| 1997-01-01 23:30:00 | 28.3 |
| 1997-01-02 00:00:00 | 20.1333333333333 |
| 1997-01-02 00:30:00 | 29.3 |
| 1997-01-02 01:30:00 | 27.5666666666667 |
| 1997-01-02 02:30:00 | 43.4166666666667 |
which is the closest I've gotten :-)
In another post ( https://stackoverflow.com/a/6560742/1142735 ) it was suggested that GROUP BY FLOOR(MOD((mytimestamp-1800)/3600)) would create intervals starting on the half hour if timestamp was used. I am using datetime.
Thanks
Paul
Anything that uses the DATE() function will fail to correctly group the interval 23:30 - 00:30.
Use:
FLOOR((UNIX_TIMESTAMP(date) - 1800) / 3600)