Relative Dates in query - sql

I have this query for elasticsearch SQL. It seems to be standard SQL. Here is the code:
SELECT * FROM index WHERE date > '00:00 2019-07-31'
Instead of that, I want it to be relative, so the query can be run every day and show the last 30 days. Is this possible?

I think you should be able to use current_date and interval to accomplish what you want.
SELECT * FROM index WHERE date > CURRENT_DATE - INTERVAL '30' DAY
See: https://www.elastic.co/guide/en/elasticsearch/reference/6.7/sql-functions-datetime.html

Related

Hive SELECT records from 1 hour ago

I have a hive table that contains a column called timestamp. The timestamp is a bigint field generated from java System.currenttimemillis(). I suppose it should be in UTC. Right now I am trying to select records from 1 hour ago. I know in MySQL you can do something like:
SELECT * FROM table WHERE datetimefield >= DATE_SUB(NOW(), INTERVAL 1 HOUR)
In hive, it seems like NOW() is missing. I did some searching and find unix_timestamp(). I should be able to get the current UTC time in milliseconds by doing a unix_timestamp()*1000.
So if i want to get records from 1 hour ago I am thinking about doing something like:
SELECT * FROM hivetable WHERE datetimefield >= (unix_timestamp()*1000-3600000);
Can someone suggest if it's the right way to approach this problem? Also what if I want to select like 1 day ago? Seems inconvenient to convert that to milliseconds. Any help or suggested readings will be highly appreciated. Thanks in advance for your help.
Yes unix_timestamp() gets you the seconds elapsed since Unix epoch. You can subtract 60*60*1000 milliseconds and compare your field to get the desired records.
For Hive 1.2.0 and higher you can use current_timestamp
select *
from hivetable
where
datetimefield >= ((unix_timestamp()*1000) - 3600000);
For 1 day,convert the milliseconds to date format and use date_sub
select *
from hivetable
where
from_unixtime(unix_timestamp(datetimefield,'MM-dd-yyyy HH:mm:ss')) >=
date_sub(from_unixtime(unix_timestamp()),1);

How to run PostgreSQL Query every day with updated values?

New to SQL, but trying to learn/do a job for a friend. I have a query set up that returns the number of bookings for the day. Example snippet:
...
WHERE be.event IN ('instant_approve', 'approve') AND TO_CHAR(be.created_at, 'yyyy-mm-dd') BETWEEN '2017-06-26' AND '2017-06-26';
Now, this query is set up for just today. How can I set this up so that tomorrow the query is executed for '2017-06-27' and so on? Is this possible?
Built-in function now() gives you a timestamp of the beginning of your transaction (CURRENT_TIMESTAMP pseudo-constant is its "alias", a part of SQL standard, but I prefer using the function).
Another function, date_trunc() gives you a "truncated" timestamp and you can choose, how to truncate it. E.g., date_trunc('day', now()) will give you the date/time of beginning of the current day.
Also, you can add or subtract intervals easily, so here is an example that gives you the beginning of the current and the next days:
select
date_trunc('day', now()) cur_day_start,
date_trunc('day', now() + interval '1 day') as next_day_start
;
Also, I would not use to_char() or anything else on top of created_at column -- this will not allow Postgres planner use index on top of this field. If you do want to use to_char(), then consider adding a functional index over to_char(created_at, 'yyyy-mm-dd').
Here is how I would retrieve records generated at July 26, 2017:
where
created_at between '2017-06-26' and '2017-06-27'
(implicit type casting from text to timestamps here)
This is equivalent to
where
created_at >= '2017-06-26'
and created_at <= '2017-06-27'
-- so all timestamps generated for July 26, 2017 will match. And for such query Postgres will use a regular index created for created_at column (if any).
"Dynamic" version:
where
created_at between
date_trunc('day', now())
and date_trunc('day', now()) + interval '1 day'
Use current_date built-in function in the between condition and it will work only for today's bookings.
.........
WHERE be.event IN ('instant_approve', 'approve') AND TO_CHAR(be.created_at, 'yyyy-mm-dd') =current_date;

Filter Data for 30 months using subquery with INTERVAL function in Teradata

I would like to filter out the data using a sub query in the interval function
Following is the query i use
SEL * FROM my_table WHERE MY_DATE < CURRENT_DATE- INTERVAL '30' MONTH;
The above query works, However i want to parameterize the period '30' using a sub query. Please suggest how to achieve this.
Thanks in Advance
Don't use interval calculations with year/month as it will fail, e.g. DATE '2016-12-31' + INTERVAL '30' MONTH results in 2019-06-31 (according to Standard SQL) which obviously doesn't exist.
SELECT *
FROM my_table
WHERE MY_DATE < ADD_MONTHS(CURRENT_DATE, (SELECT -col FROM tab));
If col is actually an INTERVAL you need to cast it to an INT.

Match partial date in SQL query?

I am looking for a way to match a date, with the one related to records on my db.
I know how to match strings with the LIKE operator, but it doesn't work for dates; unless I search just for a number (say all records at 10, all records from the 21st and so on). As soon as I put minutes for example, the search with LIKE return no records.
How do you actually search for partial date? My objective is to find all records newer than a partial date; in the current day.
select * from CLIENTS where CLOSING like '%12:30%'
This won't match anything; but if I use just 12 or 30, it works....although it is not doing what I want.
Do I have to convert date in strings? I get a partial date from another process, and I would like to match all the records newer than the partial date.
Try this query
select * from CLIENTS where
TO_CHAR(CLOSING , 'dd-mm-yy hh24:mi:ss') like '%12:30%'
or
select * from CLIENTS where
TO_CHAR(CLOSING , 'hh24:mi') = '12:30'
If you want loans more recent than today at 12:30pm, you are best served to use date arithmetic rather than relying on string conversion
SELECT *
FROM clients
WHERE closing >= trunc(sysdate) + interval '12' hour + interval '30' minute;
or
SELECT *
FROM clients
WHERE closing >= trunc(sysdate) + 12.5/24
Here, trunc(sysdate) returns today at midnight. Then you can either add a fractional number of days (1/24 adds one hour so 12.5/24 takes you to 12:30pm) or you can add one or more intervals (you could use to_dsinterval as well to create the interval).

current_date usage

I would like to subtract the current date from a given date in SQL or in JDBC. I would like to have the result in hours. Not sure how to treat the date in that case. Can some one give me a basic example Please.
You don't mention which database server you use - here's a sample in MySQL.
select hour(TIMEDIFF('2011-03-15 19:59:59.000001', now()))
Note: the "hour" function doesn't deal with rounding, so if you need that, you need to do some further arithmetic.
Date functions are pretty vendor-specific, so your mileage may vary....
In standard SQL
select (date '2011-03-16' - CURRENT_DATE) as days_different,
(date '2011-03-16' - CURRENT_DATE) * 24 as hours_different
days_different hours_different
--
1 24
As I write this, CURRENT_DATE = '2011-03-15'.