Selecting all rows before a given time in SQL - mysql

I have a fairly simple SQL table (using MYSQL Workbench 8.0) where the rows of the table contain dates and values like so:
date | value
--------------------|----------
2018-09-06 18:00:00 | 73
Values in the date column range from %18:00:00 to %17:30:00.
All I would like to do is return results from a query where I exclude all rows where the time in the date column is before 17:00:00.
The query I am currently using to no avail is:
SELECT * FROM table_name where td_time <> '%17%'
For reference, the values in column 'date' are formatted as type datetime.
I believe I'm missing something fairly simple, but this is my 2nd day using SQL and I cannot figure out if I am missing a small nuance to the syntax.

There are many function in MySQL to process time and date value, you can do like this:
select * from table where hour(date) < 17 and hour(date) > 17
Or
select * from table where time(date) < '17:00:00' and time(date) > '18:00:00'

Related

Pyspark subquery on the same table

I have some data that has year, month, date, column_x. The column_x can be missing or not missing. What I want to generate is the missing rate of column_x. In order to do so, I'm trying to create two columns that contains the total row number, which would be total_count, and count column, that represents the column_x == null.
I'm trying to create something like below:
total_count | count | year | month | date
60 | 20 | 2022 | 12 | 01
so I can do in future count / total_count to get some percentage.
However, I'm not sure how I can generate a query.
I tried subqueries but it's throwing me an error.. how can I achieve this through pyspark or sql subqueries? (I can register temp table and run sql queries as well)
What I want to generate is the missing rate of column_x.
You can do conditional counts. In MySQL:
select year, month, day,
count(*) as cnt_total,
count(column_x) as cnt_x_not_null,
sum(column_x is null) as cnt_x_null,
avg(column_x is null) as ratio_x_null
from mytable
group by year, month, day
The last expression (avg) gives you the ratio of rows where the column is null. This works because MySQL evaluates conditions as 0/1 in numeric context, so we can just use avg on top of the is null predicate.
Other columns in the resultset give more examples of conditional counts.

How can I search a column in MySQL containing unix timestamps?

I have mysql table called user_log that contains something like:
userid created_at
1 1388514600
2 1391193000
I want to get the record using exact date. For example I have the created_at which is 2013-12-31
SELECT * FROM user_log WHERE created_at = UNIX_TIMESTAMP('2013-31-12');
But record is not selected, I don't know what was the problem in that query. So how can I get the record from unix timestamp column in mysql using date?
To know the exact value in dateformat, you can do :
SELECT FROM_UNIXTIME(created_at) FROM user_log;
That said, the value 1388514600 is not just 2013-12-31 but it's actually 2013-12-31 18:30:00;
So to search by just date, you can try this:
SELECT FROM_UNIXTIME(created_at) AS dt
FROM user_log
HAVING date(dt)='2013-12-31';
Fiddle here

MSAccess: Return query for specific date from datetime value

I am trying to run a query from a linked table in MS SQL Server that has a datetime field. I'm trying to run a simple query to search for records for a specific date (#03/24/2018#), but since the datetime field has the time as well, I am not getting any records unless I specify the time range using BETWEEN with the time (Between #03/24/2018 00:00:00 AM# And #03/24/2018 11:59:59 PM#).
Original query which does not return desired output:
SELECT *
WHERE MyDateTimeField) = #3/24/2018#;
Query
SELECT *
WHERE MyDateTimeField) Between #3/24/2018 00:00:00 AM# And #3/24/2018 23:59:59#);
Is there a workaround to this as to not have to use a BETWEEN operator with the time?
To avoid time portion, check forMyDateTimeFieldequal/greater than searched day and less than next day:
SELECT *
FROM MyTable
WHERE
MyDateTimeField >= #3/24/2018#
AND
MyDateTimeField < DateAdd("d",1,#3/24/2018#);
In opposite of convertingMyDateTimeFieldto date, this does not prevent index usage and handlesNullValues onMyDateTimeField.

fetch last 5 minutes of data from oracle table

Hi i am trying to fetch last 5 minutes of data from oracle table.The query is written below and its not working somehow.
select * from mytable where (time_to_sec(timediff(now(),mytable.time_stamp)) <= 300)
Its showing this error ORA-00904.
I tried one more query.
select * from mytable where TIME_STAMP > (sysdate - numtodsinterval(5,'minute'))
Now, can you tell me the query which fetches data of last 5 minutes and which deletes data that is in the table for more than 12 hours.Thanks.
I need queries in both oracle and mysql. The mysql query i tried is here.
delete from mytable where (time_to_sec(timediff(now(),time_stamp))/3600 >12);
In oracle subtracting 1 from timestamp means one day. And You can substract a fraction of one. So,
current_timestamp - (5/(24*60))
gives You date from 5 minutes ago. Using that we can query:
select * from mytable where TIME_STAMP > current_timestamp - (5/(24*60)
Which should give You needed result. I find this method more straightfoward and simpler to remember than using special functions.
If You want filter out data from last 12 hours than You can query it like this:
select * from mytable where TIME_STAMP <= current_timestamp - 0.5

MySQL Date in where clause

I have a table which contains date (Field Type: Date and Date Format: %Y-%m-%d) as a field. I need to select all the rows from the table for all the years whose date is not between Dec 3rd and Dec 24th.
The table contains month and day as a separate fields.
The result can be obtained by using the following query:
select * from mytable where date not in (select date from mytable where month=12 and day between 3 and 24);
But i m trying to get the result in a single query like the below one but it gave empty rows:
select * from mytable where date not between '%Y-12-03' and '%Y-12-24';
Can it be done in a single query like the above one?
SELECT *
FROM mytable
WHERE MONTH(`date`) <> 12
OR DAY(`date`) NOT BETWEEN 3 AND 24
;
This will give you every row that meets the requirements. I'm sure someone has a faster way of doing this, since this will ignore all indexes and will likely be slow on a large dataset, but it does work and return the data you require, so if no-one can suggest an improvement this will answer your question.