MySQL: how to select rows by date every n days - mysql

I need to get all rows, by an ISO datetime field, EVERY "n" (eg. 180) days (note: NOT only the last 180 days).
For example, I need to select every single row at 180 days, 360 days, and so on.
So, for example, if I have a record with date 2016-01-11 12:30:00, I'll get this record when DATE() is 2016-07-09, 2017-01-05, 2017-07-04, and so on.

Use DATEDIFF function of MySQL
Demo: http://rextester.com/AEIN10581
set #QUERY_DATE='2016-07-09';
SELECT * FROM `dates`
WHERE DATEDIFF(`date`, #QUERY_DATE) % 180 = 0;
In place of #QUERY_DATE, you can use CURDATE() for current date.

Related

Find How many minutes a month has: MySQL

Say I have a column
Date
23-03-2019
04-04-2019
I want to find hoe many minutes the whole month has in MySQL.
Expected output:
Date MinsinMonth
23-03-2019 44640
04-04-2019 43200
Basically, you just want to find the number of days in the month and then do some multiplication. For this, use last_day():
select day(last_day(date)) * 24 * 60 as minutes_in_month
This should work:
SELECT DAY(LAST_DAY(Date)) * 1440 AS MinsinMonth
LAST_DAY returns the last day in the month a date is in
DAY Returns the day number associated to a date
1440 is the number of minutes per day (60 * 24)

get certain date, preventing holidays or sundays stored on database

i have column datetime format with consecutive dates, but i need count 60 days ago from now but i need prevent days marked with 0 because days are sundays or holidays.
this not work:
SELECT DateDVH as refDays
FROM DVH_days
WHERE DateDVH>'2016-12-20 00:00:00'
AND (DateDVH_dia<'2017-02-18 00:00:00' + INTERVAL 60 DAY)
AND DateDVH_daytype!=0
DateDVH_daytype=0 is holidays and sundays.
It is possible to place a counter in the query or a condition that prevents the days type = 0 or a stored procedure??
update
i solve it with :
SELECT DateDVH_dia
FROM Avipac_DVH_dias
WHERE DateDVH_dia>now()
AND DateDVH_diatype!=0
LIMIT 60
but i get a list of 60 date and only need get the last date

how to retrieve records from mysql which are inserted in 30mins interval?

I have a requirement of counting the no. of records inserted into a table for every half an hour.say from 11 to 11 30 if there 5 records and 11 30 to 12 if there are 4 records how to find the no. of records
You'd need the datetime each row was inserted; it's easiest if that is a column in the table. (We'll assume here that the column is named inserted_dt.)
All that we really need is an expression that operates on inserted_dt to return a single value for every value within a given half hour.
If we needed "hour" intervals, and not "half-hour" intervals, it would be very easy:
DATE_FORMAT(t.inserted_dt,'%Y-%m-%d %H:00:00')
Let's define the first "half-hour" ranges as minutes >= '00' AND minutes < '30'
To get the "minutes" out of the inserted_dt column, we could use either of
EXTRACT(MINUTE FROM t.inserted_dt)
DATE_FORMAT(t.inserted_dt,'%i')
We can use a conditional test to determine whether the minutes value is less than 30, or flip it around and test for greater than or equal to thirty:
DATE_FORMAT(t.inserted_dt,'%i')+0 >= 30
We can put that back together with the "year-month-day-hour", by adding an interval of either 0 or 30 minutes,
DATE_FORMAT(t.inserted_dt,'%Y-%m-%d %H:00:00')
+ INTERVAL 30*(DATE_FORMAT(t.inserted_dt,'%i')+0>=30) MINUTE
(There are lots of expressions we could use to do something similar; this one is just one of the shortest we can use to return a DATETIME datatype
Now, we just add the expression to the SELECT list of our query, we get a value that identifies the "halfhour".
To get a "count" for each half hour range, that's just a simple COUNT() aggregate and a GROUP BY. The "trick" is that we use the new "halfhour" expression in the GROUP BY clause.
SELECT DATE_FORMAT(t.inserted_dt,'%Y-%m-%d %H:00:00')
+ INTERVAL 30*(DATE_FORMAT(t.inserted_dt,'%i')+0>=30) MINUTE AS halfhour
, COUNT(*)
FROM mytable t
GROUP BY halfhour
Obviously, add a WHERE clause if you only want to return results for a specified datetime range,
SELECT DATE_FORMAT(t.inserted_dt,'%Y-%m-%d %H:00:00')
+ INTERVAL 30*(DATE_FORMAT(t.inserted_dt,'%i')+0>=30) MINUTE AS halfhour
, COUNT(*)
FROM mytable t
WHERE t.inserted_dt >= '2014-08-12'
AND t.inserted_dt < '2014-08-12' + INTERVAL 1 DAY
GROUP BY halfhour

Grouping timestamp field by date

I am trying to write an SQL query to return how many links were submitted to my website over the last 7 day period. So far I have this:
SELECT COUNT(`id`) AS `count`
FROM `links`
WHERE `created` > NOW() - 86400
AND `created` < NOW()
this works for one day, it returns one row called count with the number of links submitted in the last 24 hours. I need to change it to return 2 columns called date and count, with 7 rows (one for each day).
The tricky part that I can't get my head around is that created is a timestamp column, and I don't have access to change it so I have to work with it.
Edit: work in progress for the query:
SELECT DAY(FROM_UNIXTIME(created)) AS day, COUNT(id) count
FROM links
GROUP BY DAY(FROM_UNIXTIME(created))
LIMIT 7
NOW() actually shouldn't be working as it returns a datetime. Also, if you want to fetch 7 days worth of data, you want to subtract 604800 from UNIX_TIMESTAMP(). You can use then date and time functions with FROM_UNIXTIME. This will make grouping easier. Optimally, your column should be of datetime type.
It would go something like:
SELECT DAY(FROM_UNIXTIME(created)) day, COUNT(id) count
FROM links
WHERE created > UNIX_TIMESTAMP() - 604800 AND created < UNIX_TIMESTAMP()
GROUP BY DAY(FROM_UNIXTIME(created))
You can alternatively use the BETWEEN operator:
WHERE created BETWEEN UNIX_TIMESTAMP() - 604800 AND UNIX_TIMESTAMP()
See the demo

How to Subtract Days in MySQL

How can I subtract time in MySQL? For example, today is 16 March; I want to subtract 15 days to reach 1 March. Are there any methods that can be used to subtract 15 days from the current date?
SELECT DATE(NOW()-INTERVAL 15 DAY)
For a list of units see http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add
Not entirely related to this question but is related to the title:
SELECT SUBTIME("10:24:21", "5"); -- subtracts 5 seconds. (returns "10:24:16")
SELECT SUBTIME("10:24:21", "01:00:00"); -- subtracts one hour. (returns "09:24:21")
Documentation: MySQL SUBTIME function
Use:
SELECT NOW() - INTERVAL 15 DAY
to keep the datetime precision.
You can use this :
SELECT DATE(NOW()-INTERVAL 15 DAY);
for when you want to subtract the number of days.
In order to subtract the time instead, say 15 minutes, the following should work:
SELECT(DATE_SUB(NOW(), INTERVAL '15:0' MINUTE_SECOND));
Adding the reference link again :- https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-add.
Yes its possible using date function in Mysql
select distinct
lastname,
changedat, date_add(changedat, interval -15 day) as newdate
from employee_audit;
lastname and changedat is field name and employee_audit is table name.
I have subtract 15 days from my date - check image please. thanks