I have a range of dates like
2015-05-31 17:36:36.000000
to
2015-12-20 22:20:00.000000.
well, i want fetch rows which are between 17 and 19 of each day in that period of time. of course it is important that include minutes too.
i mean, in every single day, if a row has a time which is between 17 to 20, i need that.
What query should i use?
i used
SELECT *
FROM (`orktape`)
WHERE STR_TO_DATE(timestamp, '%Y-%m-%d %H:%i:%s') >= '2015-05-31 17:36:36.000000'
AND STR_TO_DATE(timestamp, '%Y-%m-%d %H:%i:%s') < '2015-12-20 22:20:00.000000'
ORDER BY `timestamp` desc
but it result all the rows which are between 2015-05-31 17:36:36.000000 and 2015-12-20 22:20:00.000000 which include some rows like 2015-12-20 02:20:00.000000 which is not the correct result.
Thanks.
You can select in a range of hours by...
SELECT * FROM orktape WHERE HOUR(mytimestamp) >= 17 AND HOUR(mytimestamp) < 19
Not sure what your timestamp column is called, I would not call it timestamp, that could be confusing.
Related
I have a table, that is updated every minute and I need to calculate the average value of each hour, for the values of the last 30 days.
Timestamp | SB1_AC_GES_DIFF
2020-07-14 15:13:04 30
2020-07-14 15:12:07 27
... ...
I want to save the results in a second table named avgTable like this
Timestamp | AVG_SB1
15:00 29
16:00 32
... ...
It would be perfect if the table could update itself once a day, maybe when it's 12 o'clock and the the date part for the day changes.
You can try:
INSERT INTO avg_table
SELECT Date_format(Timestamp, "%h:00:00") AS HourlyTimeStamp,
Avg(sb1_ac_ges_diff) AS "AVG_SB1"
FROM table
WHERE Timestamp between DATEADD(DAY, -30, GETDATE()) AND GETDATE()
GROUP BY 1
Assuming that you want the average rolling average, agnostic of the day.
You can just use the hour() function:
select hour(timestamp) as hh, avg(sb1_ac_ges_diff)
from t
group by hh;
You can convert this to a string or time if you want, but that does not seem useful to me.
If you actually want the hour for each day, then:
select date(timestamp) as dd, hour(timestamp) as hh, avg(sb1_ac_ges_diff)
from t
group by dd, hh;
So I have a problem to select data from my database. I want to select data with recorded time between 4 PM and 10 PM for a whole month. If it's isn't possible should I query per day for a whole month?
Assuming mydt column is DATETIME or TIMESTAMP datatype,
we could do something like this:
FROM t
WHERE t.mydt >= '2019-09-01'
AND t.mydt < '2019-09-01' + INTERVAL 1 MONTH
AND DATE_FORMAT(t.mydt,'%H') >= '16'
AND DATE_FORMAT(t.mydt,'%H') < '22'
The first two conditions restrict values to the month of September 2019.
The last two conditions restrict the "hour" portion of the datetime, time values from 16:00:00 thru 21:59:99
If we also want to include 22:00:00, we can use '%T' as format model in DATE_FORMAT and do a less than or equal to comparison
AND DATE_FORMAT(t.mydt,'%T') >= '16:00:00'
AND DATE_FORMAT(t.mydt,'%T') <= '22:00:00'
I need to compare sales in a period of days, across several years in my databases.
So I need to select rows with a timestamp that is in the range 22th january to 3rd february. The year can be anything.
How to do this?
example table sales:
timestamp sales_amount
2012-01-22 16:28:01 78
2012-10-11 16:28:06 90
2014-02-01 16:28:06 27
Select should get row 1 and 3, and exclude row 2 since its not between the specified days.
Try this
SELECT * FROM the_table WHERE DAYOFYEAR(`the_date_column`) BETWEEN 9 AND 34
This does not always work, but works for your required date range, because DAYOFYEAR of any date in that range is always fixed regardless of year.
As a general solution, you can play the trick, that you set the year to a specific one. For example:
select * from t
where str_to_date(date_format(timestamp, '1970-%m-%d %H:%i:%s'), '%Y-%m-%d %H:%i:%s')
between '1970-01-22 00:00:00' and '1970-02-03 23:59:59';
I'm attempting to create a select statement which gets items by year and month.
This is what I have so far:
SELECT * FROM sales WHERE YEAR(Date) = 2013 AND MONTH(?) = 'June'
I can't merely select date ranges because different months have different number days. It would be ideal to select months by their number (ie, January being 1) or a similar approach.
How is this worked out in a mysql statement?
The fields are datetime fields such as 2012-12-01 00:00:00
Have a look at the performance and write your condition as
SELECT * FROM sales
WHERE
Date >= '2013-06-01 00:00:00'
AND
Date < '2013-07-01 00:00:00'
for the example month: June of 2013
so MySQL can use an index on the column date. You will get exactly all rows with a date in the June of 2013, even those in the first second, but not those in the first second of the July of 2013.
You see, that you don't need to know the number of days of the particular month, because you will ever use the first of both months.
You could use a bit of date calculation too:
SELECT * FROM sales
WHERE
Date >= '2013-06-01 00:00:00'
AND
Date < '2013-06-01 00:00:00' + INTERVAL 1 MONTH
so you need only to know the start and the length of the interval.
Note
The most important part of my answer is to use the column Date without using a function on this column, so MySQL can use an index.
I am trying to select rows that are in between two dates. First, here is my data:
punch_id eid time unixtime punch_type day date doy
135 2 12:53 1314723193 0 4 08/28/2011 241
134 2 12:53 1314723190 3 4 08/31/2011 241
133 2 12:53 1314723187 2 4 08/20/2011 241
132 2 12:52 1314723125 1 4 08/30/2011 241
I have tried these two queries.
SELECT * FROM `punches` WHERE `date` >= '08/20/11' AND `date` <= '08/31/11'
SELECT * FROM `punches` WHERE `date` BETWEEN '08/20/11' AND '08/31/11'
Neither of these select the rows containing the date 08/31/11. It selects the 08/20/11 ones though. I tried to use it another way and when I run the query:
SELECT * FROM `punches` WHERE `date` >= '08/10/11' AND `date` <= '08/20/11'
I again do not get the correct result: the 20th is left out once again. What is the problem with the way I am executing this?
See this related question.
As others have mentioned, your primary problem is not accounting for the time. A few options to handle that:
Use a function to convert the DateTime to a Date. I don't recommend this option, since it will likely make the function non-sargeable.
Expand your BETWEEN to explicitly include the last moment of the day: (note: this is the latest possible value that MS SQL can store, don't know if MySQL has the same value)
SELECT * FROM `punches` WHERE `date`
BETWEEN '08/20/11 00:00:00.000' AND '08/31/11 23:59:59.997'
Use a < for the upper value
SELECT * FROM `punches` WHERE `date` >= '08/20/11' AND `date` < '09/01/11'
I actually think that last one is easier, in most situations.
I suppose you could do other things, like change the datatype of the column, but I've assumed here that you're just interested in changing the query.
** Disclaimer: I'm a MS SQL guy, not MySQL
I don't know mysql, but in other RDBMS, dates are assuming a time part of 12 AM. If you want to include the high date, add 1 to the day.
Is your date field of type DATE or DATETIME?
It probably has to do with the time of day. If you have a DATETIME stored as
2011-08-31 13:00:00
then it won't match on
BETWEEN '08/20/11' AND '08/31/11'
You'd have to use
BETWEEN '08/20/11' AND '09/01/11'
The MySQL docs for between say it matches "less than or equal to" max, so it is probably the time of day throwing you off.
When you don't specify a time with the date, then 00:00:00 is implied. Therefore the real query that the database is doing is more like...
SELECT * FROM `punches` WHERE `date` BETWEEN '08/20/11 00:00:00' AND '08/31/11 00:00:00'
Therefore a punch on 08/31/2011 at 12:53 will not get included. I think this should work, and is a bit more elegant than adding a day to the end date...
SELECT * FROM `punches` WHERE DATE(`date`) BETWEEN '2011-08-20' AND '2011-08-31'