Select all data from the last 5 days - sql

In mysql I need to obtain all the last 5 days records.
So if I have
Name date
aaaa 20/11/2010
dddd* 24/11/2010*
bbbb 22/11/2010
cccc 23/11/2010
eeee* 25/11/2010*
ffff* 26/11/2010*
I need only the last 5 days records.
I tried something like:
SELECT name,date
from Lineas
WHERE date >= DATE_SUB(CURDATE(), INTERVAL 5 DAY)
ORDER BY date DESC
but it isnĀ“t working....

If the problem is "records from the future" then you simply need to restrain your results a bit more than you've already done:
SELECT name,date
from Lineas
WHERE date >= DATE_SUB(CURDATE(), INTERVAL 5 DAY) AND date <= CURDATE()
ORDER BY date DESC

Have you tried between
SELECT name,
date
from Lineas
WHERE date BETWEEN DATE_SUB(CURDATE(), INTERVAL 5 DAY) AND CURDATE()
ORDER BY date DESC

Related

Search Last 7 days excluding today Oracle SQL

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.

Get data of last Month day by day in oracle sql

I want to get data from last month day by day, I can get the last 30 days but I just want the month as it may be less or more than 30 days,
this is the query for getting the last 30 days
SELECT Trunc(timestamp),
Count(*)
FROM table1
WHERE Trunc(timestamp) > Trunc(sysdate - 30)
GROUP BY Trunc(timestamp)
ORDER BY 1;
Also, I am using it in a shell script if I can make a variable in the script and put it the query
To get data from the start of the current month until today:
SELECT TRUNC(timestamp) AS day,
COUNT(*)
FROM table1
WHERE timestamp >= TRUNC(SYSDATE, 'MM')
AND timestamp < TRUNC(SYSDATE) + INTERVAL '1' DAY
GROUP BY TRUNC(timestamp)
ORDER BY day
To get data from the same day last month until today:
SELECT TRUNC(timestamp) AS day,
COUNT(*)
FROM table1
WHERE timestamp >= ADD_MONTHS(TRUNC(SYSDATE), -1)
AND timestamp < TRUNC(SYSDATE) + INTERVAL '1' DAY
GROUP BY TRUNC(timestamp)
ORDER BY day
db<>fiddle here

BigQuery Where Date is Less Than or Equal to 3 Days Minus Current Date

I'm trying to create a query to only return data where date is minus 3 days from the current date. I've tried:
date <= DATE_ADD(CURRENT_DATE(), -3, 'DAY')
But this returns Error: Expected INTERVAL expression
See WHERE clause in below example
#standardSQL
WITH yourTable AS (
SELECT i, date
FROM UNNEST(GENERATE_DATE_ARRAY('2017-04-15', '2017-04-28')) AS date WITH OFFSET AS i
)
SELECT *
FROM yourTable
WHERE date <= DATE_SUB(CURRENT_DATE(), INTERVAL 3 DAY)
-- ORDER BY date
Btw, in case if you are still with Legacy SQL - see below example
#legacySQL
SELECT *
FROM -- yourTable
(SELECT 1 AS id, DATE('2017-04-20') AS date),
(SELECT 2 AS id, DATE('2017-04-21') AS date),
(SELECT 3 AS id, DATE('2017-04-22') AS date),
(SELECT 4 AS id, DATE('2017-04-23') AS date),
(SELECT 5 AS id, DATE('2017-04-24') AS date),
(SELECT 6 AS id, DATE('2017-04-25') AS date)
WHERE TIMESTAMP(date) <= DATE_ADD(TIMESTAMP(CURRENT_DATE()), -3, 'DAY')
-- ORDER BY date
This works with a string formatted date.
DATE(TIMESTAMP(date)) <= DATE_SUB(CURRENT_DATE(), INTERVAL 3 DAY)
Just tested this and seems to work.
I added this :
and DATE(TIMESTAMP(datevalue)) >= DATE_SUB(CURRENT_DATE(), INTERVAL 21 DAY)
and managed to get all records greater than last 21 days worth. Only thing I changed from #ericbrownaustin 's code was changed the 'date' in the first piece of code in the second set of parenthesis.

How many records created for each day of the week this year?

I have about 50k rows in a Postgres database that are users and when they signed up.
I am trying to understand how many users sign up for each day of the week since the start of the year, e.g.:
1238 on Monday
3487 on Tuesday
1237 on Wednesday
Example date entry: '2014-10-31 17:17:30.138579'
A plain aggregate query after extracting the weekday. You could use to_char() to get the (English by default) weekday:
SELECT to_char(created_at, 'Day'), count(*) AS ct
FROM tbl
WHERE created_at >= date_trunc('year', now())
GROUP BY 1;
If performance is important, EXTRACT() is slightly faster:
SELECT EXTRACT(ISODOW FROM created_at), count(*) AS ct
FROM tbl
WHERE created_at >= date_trunc('year', now())
GROUP BY 1;
1 .. Monday, ... , 7 .. Sunday.
You can use the EXTRACT(DOW from timestamp) to determined the day of the week. 0 is Sunday. 6 is Saturday.
Example:
SELECT EXTRACT(DOW FROM TIMESTAMP '2015-06-22 20:38:40');
Result is 1 (Monday)

Best way to get recent date rows? (this week/month)

I wanna get rows in recent date, this week or month, etc. suppose the table has a field named: product_date.
To get the rows in the last month, you could use something like:
SELECT * FROM table WHERE product_date >= DATE_SUB(NOW(), INTERVAL 1 MONTH);
Or for the last week:
SELECT * FROM table WHERE product_date >= DATE_SUB(NOW(), INTERVAL 1 WEEK);
For within the last seven days:
WHERE product_date BETWEEN DATE_ADD(NOW(), INTERVAL -7 DAY) AND NOW()
For the last month:
WHERE product_date BETWEEN DATE_ADD(NOW(), INTERVAL -1 MONTH) AND NOW()
While mopoke's solution gets results from last 30 or 7 days, this gives you results for current month only:
SELECT * FROM table WHERE DATE_FORMAT(product_date, '%c-%Y') = DATE_FORMAT(CURDATE(), '%c-%Y');
Or for current week:
SELECT * FROM table WHERE DATE_FORMAT(product_date, '%u-%Y') = DATE_FORMAT(CURDATE(), '%u-%Y');