I want to query all products sold in the last 5 years.
It is possible to do it like this:
select * from products
where time between sysdate-1826 and sysdate
But it there also a nicer way instead of calculating all the days and subtract it from sysdate?
SELECT *
FROM products
WHERE date_column >= add_months( sysdate, -12*5 )
or
SELECT *
FROM products
WHERE date_column >= sysdate - interval '5' year
will both give you all the rows from the last 5 years (though you would generally want to add a TRUNC to remove the time portion unless you really care whether a row was created on Feb 8, 2007 in the morning or in the afternoon).
select * from products
where time > DATE_SUB(NOW(), INTERVAL 5 YEAR)
Date sub will subtract 5 years from now
Related
I have the below code to which I want to return the last 7 days excluding today (for example from 5th May - 11th May as opposed to 5th May - 12th May)
What else would I be able to include to acheive this?
SELECT *
FROM TABLE_1
WHERE DATE_TIME >= SYSDATE -7
You want to have a range that starts from 7 days before midnight today and ends before midnight today:
SELECT *
FROM table_name
WHERE date_time >= TRUNC(sysdate) - 7
AND date_time < TRUNC(sysdate);
This should work:
SELECT *
FROM TABLE_1
WHERE DATE_TIME >= SYSDATE -7
AND TRUNC(DATE_TIME) != TRUNC(SYSDATE)
The TRUNC is needed to strip the time portion of the date column and sysdate.
Note that DATE_TIME >= SYSDATE -7 will include the time portion of SYSDATE and substract 7 days. If you run the query at 10AM, do you want to include rows that have date_time = sysdate - 7 at 9AM too ? If so it is better to add a TRUNC there too DATE_TIME >= TRUNC(SYSDATE) -7.
I'm trying to exclude the Christmas period between two dates every year in my database. This is the query I'm trying to run. From my understanding this should include every row, for every year with the exception of rows where the month is <= 12 and day <= 15 and where the month is <= 12 and day >= 26.
The result, however, doesn't appear to return any rows at all!
SELECT
*
FROM
transactons
WHERE
event_date BETWEEN TO_DATE("2016-12-01")
AND TO_DATE("2021-12-31")
AND ((EXTRACT(MONTH FROM event_date) <= 12 AND EXTRACT(DAY FROM event_date) <= 15)
AND (EXTRACT(MONTH FROM event_date) <= 12 AND EXTRACT(DAY FROM event_date) >= 26))
Does anyone have any suggestions as to how to filter out Dec-15 to Dec-26 each year here?
You should specify your DBMS, because the following code can change. Please provide it and I will glade to update my answer.
For something quick and if I understand your logic:
SELECT
*
FROM
transactons
WHERE
(
MONTH(event_date) = 12
AND
DAY(event_date) BETWEEN 12 AND 26
)
You can reverse the query if you put NOT after the WHERE
I have a query (in Oracle SQL Developer) which is currently hard-coded to select all records from 2019, 2018, and 2017. Now that it's 2020, I'd like to change this to select going back three years dynamically, so that this query can work years from now without having to change the code.
I know that I can find all records from this specific date last year through the end of the year with -
SELECT * FROM TABLE
WHERE BOOKDATE >= add_months( sysdate, -12*1 );
So that would give me from 11-MAR-2019 through the end of 2019. But how do I select all records for the current year, last year, and two years ago (each year separately) - and not from this date specifically? BOOKDATE has the format DD-MON-YY.
You could extract the year and perform the calculation on it:
SELECT *
FROM mytable
WHERE EXTRACT(YEAR FROM bookdate) - EXTRACT(YEAR FROM SYSDATE) <= 3
You could try like this:
SELECT *
FROM TABLE
WHERE EXTRACT(YEAR from BOOKDATE) >= EXTRACT(YEAR from sysdate)-3
I am writing a query that counts trips which exceed 20 minutes but is only in the years 2015, 2016, 2017. The code works fine, showing the years and number of trips per year, but the results show all years and not just these three. The issue is that the column start_time is a timestamp. I can do timestamp_add and then between (as shown below, disregard the number of days as they are just placeholders) but it just seems sloppy
I can do timestamp_add and then between (as shown below, disregard the number of days as they are just placeholders) but it just seems sloppy.
SELECT extract(year from start_time), count(*)
FROM `datapb.shopping.trips`
where duration_minutes > 20 and
start_time between timestamp_add(current_timestamp(), interval -1000 DAY) AND timestamp_add(current_timestamp(), interval -350 DAY)
group by EXTRACT(YEAR from start_time)
Any suggestions would be fantastic, thanks!
Why not just use timestamp constants?
select extract(year from start_time) as yyyy, count(*)
from `datapb.shopping.trips`
where duration_minutes > 20 and
start_time >= timestamp('2015-01-01') and
start_time < timestamp('2018-01-01')
group by yyyy
order by yyyy;
Im using the following query to target results that are exactly X days older than current time.
SELECT *,
DATE_FORMAT(datetime, '%m/%d/%Y')
FROM table
WHERE datetime BETWEEN SYSDATE() - INTERVAL 30 DAY
AND SYSDATE()
ORDER BY ID DESC
Problem is its returning data from current day if a record from exactly 30 days ago doesnt exist, as well as irrelevant data
is there a better way of doing this?
BETWEEN includes all values in between the two arguments, including the value at each end. In other words, BETWEEN 1 AND 4 includes values 1, 2, 3, and 4. Not just 1 and 4, and not just 2 and 3.
If you just want dates from the single day that is 30 days ago, try this:
SELECT *,
DATE_FORMAT(datetime, '%m/%d/%Y')
FROM table
WHERE DATE(datetime) = CURDATE() - INTERVAL 30 DAY
ORDER BY ID DESC
Use CURDATE() instead of SYSDATE() because CURDATE() returns a date without a time component.
Your query is set to obtain records between today (including time) and 30 days previous.
If you want records that are older than 30 days (to the time), use:
SELECT *,
DATE_FORMAT(datetime, '%m/%d/%Y')
FROM table
WHERE datetime <= DATE_SUB(SYSDATE(), INTERVAL 30 DAY)
ORDER BY ID DESC
If you want those that are only 30 days old, not 31 or 29, without respect for the time portion - use:
SELECT *,
DATE_FORMAT(datetime, '%m/%d/%Y')
FROM table
WHERE DATE_FORMAT(datetime, '%m/%d/%Y') = DATE_FORMAT(DATE_SUB(SYSDATE(), INTERVAL 30 DAY), '%m/%d/%Y')
ORDER BY ID DESC