How to get records from table which has timestamp starting from particular date of previous month to particular date of this month in postgres? - postgresql

I have a customer_visit table, my requirement is to get the records of previous month starting from 3rd date to current month 2nd date and which contains flag 'Y'. How can I achieve this in Postgres. Following is the sample of my table. Any help is appreciated.

Use make_date() function from postgres to construct the date and query on it.
select * from customer_visit
where created_time > make_date(2018, date_part('month', now())::int - 1, 03)
and created_time <= make_date(2018, date_part('month', now())::int, 02)
and flag = 'Y';
You can use date_part('year', timestamp) to extract year also from current date.

Related

How to select data between two dates using only the start date?

I have problem select data between two dates if the only start_date is available.
The example I want to see is what discount_nr was active between 2020-07-01 and 2020-07-15 or only one day 2020-07-14. I tried different solutions, date range, generate series, and so on, but was still not able to get it to work.
Table only have start dates, no end dates
Example:
discount_nr, start_date
1, 2020-06-30
2, 2020-07-03
3, 2020-07-10
4, 2020-07-15
You can get the end dates by looking at the start date of the next row. This is done with lead. lead(start_date) over(order by start_date asc) will get you the start_date of the next row. If we take 1 day from that we'll get the inclusive end date.
Rather than separate start/end columns, a single daterange column is easier to work with. You can use that as a CTE or create a view.
create view discount_durations as
select
id,
daterange(
start_date,
lead(start_date) over(order by start_date asc)
) as duration
from discounts
Now querying it is easy using range operators. #> to check if the range contains a date.
select *
from discount_durations
where duration #> '2020-07-14'::date
And use && to see if they have any overlap.
select *
from discount_durations
where duration && daterange('2020-07-01', '2020-07-15');
Demonstration

Postgres query to get date and total amount grouped by day

I have a table Logs with fields
Amount,date
I need to get the sum of amount and months grouped by each day
I need to migrate my sqlite code to postgresql but i find the code migration kind of hard.The sqlite code is as follows
SELECT SUM(amount),transaction_date FROM log WHERE user_id = 1 AND strftime('%Y', transaction_date) = '2019' GROUP BY strftime('%d', transaction_date);
What i need to is date and total amount grouped by day for the year 2019
You need extract() function instead of strftime() to get the year and cast transaction_date to date only if it is timestamp, if it is of data type date remove the casting ::date:
SELECT
SUM(amount),
transaction_date::date
FROM log WHERE user_id = 1 AND extract(year from transaction_date) = 2019
GROUP BY transaction_date::date

Subtract month from date column

I would like to subtract one month from a date column in Amazon Redshift (a fork of PostgreSQL 8.0.2).
So for each date column in a table, it will add another column date_minus_a_month.
I tried this code
Select date,date::date -interval '1 month'
from table
and received an error:
Interval values with month or year parts are not supported.
Does anyone have a solution for this?
You can use datesub, although I just use dateadd for everything and use negative numbers.
eg
SELECT getdate() as right_now, dateadd(month, -1, getdate()) as last_month
Docs: https://docs.aws.amazon.com/redshift/latest/dg/r_DATEADD_function.html

DB2- select rows with date_column of last month

I need to select count(field) from table where the date_column is within the last month, i.e. from the first of the last month till the first of this month. As this query will be automated it will need to refer to the last month of any given day.
Also the date_column has a datatype of timestamp
Thanks in advance!
SELECT (CAST(current timestamp AS DATE)) - (DAY(CAST(current timestamp AS DATE)) - 1)DAYS FROM sysibm.sysdummy1;
SELECT (CAST(current timestamp AS DATE)) - (DAY(CAST(current timestamp AS DATE)) - 1)DAYS
- 1 MONTH
FROM sysibm.sysdummy1;
Managed to find these queries, to find the first of this month and last month

Counting the number of sales for each date in a date range, postgresql

I have a table with a row for each sale of a product. These rows include a date. I want to know the number of products sold for each distinct day in a range (user specifies the begin and end dates.) There is a distinct row for each sale, so on days where several products were sold, several rows with this date exist. I want to count the number of these rows, with the same date. How might this be done efficiently in postgresql?
Example
2015-01-02: 0
2015-01-03: 7
2015-01-04: 2
Assuming the date column is stored as a datetime, something like this should get you in the right direction:
SELECT date_trunc('day', TIMESTAMP sales.date) as day, COUNT(*)
FROM sales
WHERE day >= start and <= end
GROUP BY day;
where start and end are filled in by the user.
More documentation for postgres's date features found here:
http://www.postgresql.org/docs/9.4/static/functions-datetime.html
If they are not datetime, you can use the to_date function with the appropriate format string to convert to datetime and the use the solution above:
SELECT date_trunc('day', to_date('2015-01-02', 'YYYY-MM-DD'));
http://www.postgresql.org/docs/9.4/static/functions-formatting.html