sql query with date intevals - sql

Hi I need help to create a query that return a result based on date interval but I can't get it to work correctly.
I would like to achieve a result giving me the records with a date that are within a historic time span:
day -1 to -7 */from yesterday and -7 days */
day -8 to -14 */the date is between -8 and -14 days from today
For the first interval I use this where clause:
...
where `invoiceExpDate` >= date_add(now(), INTERVAL - 7 DAY)
how can I modify this to NOT give me the records for today??
For the second interval I use:
...
where datediff(invoiceExpDate,now())<= 14
AND datediff(invoiceExpDate,now())> 7
AND `invoiceExpDate` > now()
I can't get them to work. CAn you help me with the correct where clause to return what I want?
Thanks

I think you can combine date_add() and BETWEEN
For your first clause
...
WHERE `invoiceExpDate` BETWEEN
date_add(now(), INTERVAL - 7 DAY) AND
date_add(now(), INTERVAL - 1 DAY)
Similar pattern for the second.

LogDate BETWEEN DATEADD(dd, -7, GETDATE()) AND
DATEADD(dd, -1, GETDATE())

Related

Presto TIMESTAMP get data from 2 days ago without inputting year month date?

My goal is to have the query grab data from 2 days ago. I don't want to have to keep inputting the date like this:
WHERE usage_start_date
BETWEEN TIMESTAMP '2020-09-09 00:00:00.000' and TIMESTAMP '2020-09-09
23:59:59.999'
but instead something like:
usage_start_date = current_date - interval '2' day
the above works for my Athena Presto SQL query, but for some reason will not give all the data that ran in those 24 hours, instead giving about half the day. Is there a way to do a statement like this one to ensure it gives ALL data in that day?
WHERE current_date - interval '2' day AND
BETWEEN TIMESTAMP '00:00:00.000' and TIMESTAMP '23:59:59.999'
without inputting the year, month, day? It seems like TIMESTAMP needs the y/m/d but what about doing a LIKE so it picks up the hour, minute, second but no need to put the y/m/d?
To get a timestamp for the start of the day that was two days ago you can do
DATE_TRUNC('day', NOW() - INTERVAL '2' DAY)
e.g.
WHERE usage_start_date >= DATE_TRUNC('day', NOW() - INTERVAL '2' DAY)
AND usage_start_date < DATE_TRUNC('day', NOW() - INTERVAL '1' DAY)
You can use below query to achieve the task by fetching the hour and date from the usage_start_date
select * from table where hour(usage_start_date) between 0 and 23 and current_date - interval '2' day = date(usage_start_date)
I would suggest:
WHERE usage_start_date >= CURRENT_DATE - INTERVAL '2' DAY AND
usage_start_date < CURRENT_DATE - INTERVAL '1' DAY

Get a previous date from current date in Druid SQL

How to get the date 7 days before from the current date in Druid SQL? I have done similar in Postgres SQL as
CURRENT_DATE - interval '7 day'
I need to do the same in Druid SQL query
you can just use the timestampadd function like this: TIMESTAMPADD(DAY, -7, CURRENT_TIMESTAMP) and you can use it within the where clause of a select statement to display records greater than or equal to 7 days as like this:
select * from "testdatasource" WHERE "__time" >= TIMESTAMPADD(DAY, -7, CURRENT_TIMESTAMP)
In case you want to round it to midnight then you can nest it with date_trunc function as follows:
DATE_TRUNC('DAY', TIMESTAMPADD(DAY, -7, CURRENT_TIMESTAMP))
Use timestamp_expr { + | - } <interval_expr> or TIME_SHIFT(<timestamp_expr>, <period>, <step>, [<timezone>]) - see the docs at https://druid.apache.org/docs/latest/querying/sql.html#time-functions which also explain the difference between the two.

Need to get data between two dates in SQL

I need to get data between two dates. here, I have added simple example as below :
Ihave added below logic in my SQL query but not working, pls help me :
like If MyDate = 2020-07-09 15:15:00
I have to run cron job. So, get those type data which datas MyDate between date of 12 hours ago and date of pending 1 hour to complete MyDate.
Pls help me to get idea using Mysql queries.
I tried this one but not getting data :
SELECT * FROM test WHERE ENDDATE BETWEEN (ENDDATE - INTERVAL 12 HOUR) AND (ENDDATE - INTERVAL 1 HOUR) ORDER BY ENDDATE DESC;
look into whereBetween
whereBetween('reservation_from', [$from1, $to1])
https://laravel.com/docs/5.6/queries#where-clauses
Guys, I got the Solution from MyEnd as below :
SELECT * FROM test WHERE ( now() > (ENDDATE - INTERVAL 12 HOUR) AND now() <= (ENDDATE - INTERVAL 1 HOUR)) ORDER BY ENDDATE DESC;

Getting interval Monday-Sunday in SQL BIGQUERY

I'm trying to get the data between last weeks monday and last week sunday. I'm having trouble with getting the relative part. I'm trying like this:
where date <= LASTWEEKSUNDAY OR date >= LASTWEEKMON
The closest I got to what I seek was using now(), but it returned also some days from the current week. Thanks in advance
You are describing:
where date >= date_sub(date_trunc(current_date, week(Monday), interval 1 week) and
date < date_trunc(current_date, week(Monday))
Although the function calls change, the same logic works on datetimes and timestamps.
Of course week(Monday) is the default for isoweek, so you can use:
where date >= date_sub(date_trunc(current_date, isoweek, interval 1 week) and
date < date_trunc(current_date, isoweek)
I think it's what you want
where date between DATE_SUB(DATE_TRUNC(CURRENT_DATE(), WEEK(SUNDAY)), interval 6 day) and DATE_TRUNC(CURRENT_DATE(), WEEK(SUNDAY))
You shouldn't use OR in the where statement, it'll cover all the days if you use OR. Instead, you can prefer using AND or between.

How to display SQL dates in the last 30 days?

I want to display the dates only in the past 30 days. for my SQL command. I have a DATETIME field called statement_to_date and I want to find all of the columns in the past 30 days for statement_to_date. Here's what I have so far:
SELECT Statement_TO_DATE, STATEMENT_FROM_DATE
FROM claim;
SELECT DATE_ADD(NOW(), INTERVAL -30 DAY)
I thought I could plug Statement_TO_DATE where INTERVAL is, but it's not working. Any ideas?
You are missing the where clause:
SELECT Statement_TO_DATE, STATEMENT_FROM_DATE
FROM claim
WHERE statement_to_date >= DATE_ADD(NOW(), INTERVAL -30 DAY); -- assumes the value is never in the future
Normally, when working with timespans in dates, you don't want the time component of the current date. So, this is more typical:
SELECT Statement_TO_DATE, STATEMENT_FROM_DATE
FROM claim
WHERE statement_to_date >= DATE_ADD(CURDATE(), INTERVAL -30 DAY);
Note that MySQL also has DATE_SUB(), if you don't want a negative time interval.
From w3schools you can see that you're not using DATE_ADD() correctly.
Also like Gordon Linoff said you're missing a WHERE clause, try:
SELECT Statement_TO_DATE, STATEMENT_FROM_DATE
FROM claim
WHERE statement_to_date >= DATE_ADD(day, -30, GETDATE());
Select DATEADD(Month, -1, getdate())