Postgres Subtract Date Interval in Having - sql

I am new to SQL but am having a (probably obvious) issue. My goal is to show all data in the month before the last dated entry. When executing the code block below to find the date a month ago,
SELECT MAX(created_at) - INTERVAL '1 MONTH' AS date
FROM shopify_view
it returns, '2019-11-27 11:40:06'. Makes sense!
But when I try to get all the date with a date above that value:
SELECT created_at AS date
FROM shopify_view
GROUP BY created_at
HAVING created_at >= MAX(created_at) - INTERVAL '1 MONTH'
ORDER BY created_at
it returns the first date as '2018-04-23 10:57:28'. Does not make sense!
what am I missing? Thank you!!

You should use a subquery
SELECT *
FROM shopify_view
Where created_at >= (SELECT MAX(created_at) - INTERVAL '1 MONTH' AS date
FROM shopify_view)
ORDER BY created_at

Related

Statistic of sales per day in the last 30 day

I have a quest that is about doing a statistic of the sales per day in the last 30 day...i've found a way to only show the last month:
SELECT *
FROM purchase
WHERE date >= date('01-05-2021', current_date - interval '1 month')
and date < date('01-05-2021', current_date)
the columns in purchase are just id, value, date, cashier and store id what do you think is the best way to do this?
i have this and i don't know way it is not working...i'm new in postgresql so please don't be offended by this
Group by date and use sum to find the total.
select date,sum(value)
from purchase
where date between current_date - interval '1 month' and current_date - 1
group by date

Select all rows with date column equal to today's date in postgres to the minute

I essentially want to run a query like:
SELECT * FROM t where date={time right now to the minute}
Use date_trunc() for your exact question:
where date = date_trunc('minute', now())
However, I suspect you really want the span of one minute:
where date >= date_trunc('minute', now()) and
date < date_trunc('minute', now()) + interval '1 minute'

PostgreSQL - Select Query between today's start time and end time

I need to query database like - SELECT FROM table_name WHERE created_at = <--BETWEEN TODAY'S START AND END DATETIME-->
Sample data from created_at column is - 2020-09-28 17:02:14
Tried method is as follows but it didn't work,
SELECT FROM users WHERE created_at=CURRENT_TIMESTAMP;
SELECT FROM users WHERE created_at=CURRENT_DATE;
Can any one help me with this query
Is this what you want?
where created_at >= current_date
and created_at < current_date + interval '1 day'
This gives you all rows whose created_at belongs to the current day.

PostgreSQL query interval data

I'm trying to perform a query which I can't figure out how to write. I have a claims table
claims table:
- id
- date_received
I want to grab the new claim count for each day for over 30 days. The naive solution I was trying to come up with was this.
select count(*)
from claims
where date_received
BETWEEN CURRENT_DATE - interval '1 month'
AND CURRENT_DATE
group by date_received;
This works but it groups by exact timestamp and not same day. How could I make it so it groups by same day?
EDIT
I was able to figure it our updated query:
select date_received::date as date, count(id) as new_claims
from claims
where date_received BETWEEN CURRENT_DATE - interval '1 month'
AND CURRENT_DATE
group by date_received::date
order by date;
Presumably, claims arrive in the past, not the future, so you would want:
select date_received::date, count(*)
from claims
where date_received >= CURRENT_DATE - interval '1 month' and
date_received < CURRENT_DATE
group by date_received::date
order by date_received::date;
Building on Gordon's answer, you can use date_trunc(precision, timestamp) to get what you want:
select date_received::date, count(*)
from claims
where date_received >=
date_trunc('day', CURRENT_DATE - interval '1 month')
and date_received < CURRENT_DATE
group by date_received::date
order by date_received::date;
See date_trunc() for details.

How to get min value of all cities of yesterday?

How to get min value(temp) of all cities of yesterday.
I want:
Indore:min value:yesterday date
Bhopal:min value:yesterday date
Mumbai:min value:yesterday date
In Postgres, you can do:
select name, min(temp)
from t
where write_date < current_date and
write_date >= current_date - interval '1 day'
group by name;
You can also write the where clause as:
where date_trunc('day', write_date) = current_date - interval '1 day'
However, using the function date_trunc() prevents the use of the index for the where clause.
select name, min(temp) from table
where date(write_date) BETWEEN TRUNC(SYSDATE - 1)
AND TRUNC(SYSDATE) - 1/86400
group by name
this will do your job