0

I am trying to automate a report that runs at the beginning of each month to query out all sales for the previous month. Given below is the query I am using but it returns null value. Could anyone help me with what I am doing wrong here.

I am using Amazon Redshift

select created_at,sale_id from billing where to_char(created_at,'yyyy-mm') = EXTRACT(month FROM CURRENT_DATE - '1 month'::interval); 

Thanks.

1 Answer 1

5

You are comparing a string like '2018-05' to a numeric value like 5 (extract returns a single number) so that is not going to work. You need to use the same values on both sides of the equal sign, e.g.

select created_at,sale_id from billing where to_char(created_at,'yyyy-mm') = to_char(CURRENT_DATE - '1 month'::interval, 'yyyy-mm'); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.