Skip to content
Advertisement

How to get data for past X weeks in redshift?

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

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

Now I am trying to make this query dynamic such that it can give me count for past 6 weeks starting from 44 to 39 not including current week something like below as an output:

So my condition for each week will be like this if I run it manually for each of those week –

Now I need to make above manual query for each of the week in a dynamic way. I came up with below query but somehow I don’t get any output back when I run it. Anything wrong I am doing in below query?

Note:

fromWeek and toWeek column are integer data type and they contain week number just fyi. It’s a legacy query so I am trying to make it dynamic. Not sure why they have single quotes around 44 and 45 but If I run my original manual query like this and (fromWeek <= '44' AND toWeek >= '45') or this and (fromWeek <= 44 AND toWeek >= 45) for week 44 I always get same data as 124.

Advertisement

Answer

Don’t you want toWeek to be 1 greater than fromWeek? If so your where clauses should be:

I suspect you just don’t have data for values for toWeek that are high enough to pass your toWeek where clause.

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