Skip to content
Advertisement

How to pull data for past x weeks dynamically in redshift?

I have a below query which I run gives me the single count for previous week which is Week 43. Current week is 44 as of now.

As of now the output I see is this which is for Week 43-

Now I am trying to make this query dynamic such that it can give me count for past 6 weeks something like below as an output:

I am able to convert above query in dynamic way which gives me the count for previous week which is 43 and it works fine but I am not sure how can I change it so that it can give me data for all past 6 weeks in the above output format.

Update

I ran below query and I am not seeing any data coming from below query –

Advertisement

Answer

Is this what you want?

Then, if you want one row per from, you can use group by. So:

Note that I modified the query to use table aliases. I would also recommend prefixing each column with the table it belongs to, so the query is unambiguous about the underlying data structure: I had no clue, so I used ??, which you need to replace with either dh or n.

I am also quite suspicious about the left join. Is there a good reason why you don’t actually want an inner join instead?

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement