如何在 redshift 中动态拉取过去 x 周的数据?

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

我有一个下面的查询,我 运行 给了我前一周的单次计数,即 Week 43。本周是第 44 周。

select
count(distinct(clientid))
from data_holder
left join name
on name.client_id = data_holder.clientid
where POC NOT IN ('SGH', 'IKU')
and status IN ('NOTTAKEN')
and (from <= '43' AND to >= '44')

截至目前,我看到的输出是第 43 周的结果-

Count
-----
124 

现在我正在尝试使这个查询动态化,这样它可以给我过去 6 周的计数,输出如下所示:

Count   Week
------------
124     W43
125     W42
126     W41
127     W40
128     W39
129     W38

我能够以动态方式转换上述查询,这让我得到了前一周的计数,即 43,它工作正常,但我不确定如何更改它以便它可以为我提供过去所有的数据以上输出格式为 6 周。

select
count(distinct(clientid))
from data_holder
left join name
on name.client_id = data_holder.clientid
where POC NOT IN ('SGH', 'IKU')
and status IN ('NOTTAKEN')
and (from <= DATE_PART(w, CURRENT_DATE) -1 AND to >= DATE_PART(w, CURRENT_DATE))

更新

我 运行 下面的查询,我没有看到来自下面的查询的任何数据 -

with data_holder as
    (
    with tree_post as
    (Select contractid as conid, max(goldennmber) as goldennmber
    from zeus.user_keys_post group by contractid)
    Select * from tree_post join zeus.user_keys_post b
    on tree_post.conid = b.contractid and tree_post.goldennmber = b.goldennmber
    ),
     
    name as 
    (
    SELECT abc, client_id, services from dim.crom c1 where c1.ver = (SELECT MAX(ver) from dim.crom c2 
    where c1.client_id = c2.client_id)
    )
     
    select from,
    count(distinct(clientid))
    from data_holder
    left join name
    on name.client_id = data_holder.clientid
    where POC NOT IN ('SGH', 'IKU')
    and status IN ('NOTTAKEN')
    and from <= date_part(w, current_date - interval '6 weeks') 
    and to   >= date_part(w, current_date)
    group by from

这是你想要的吗?

AND "from" <= DATE_PART(w, CURRENT_DATE - INTERVAL '6 weeks') 
AND "to"   >= DATE_PART(w, CURRENT_DATE))

然后,如果您希望每个 from 占一行,则可以使用 group by。所以:

select ??.from, count(distinct ??.clientid) cnt
from data_holder dh
left join name n on on n.client_id = dh.clientid
where 
    ??.poc not in ('SGH', 'IKU') 
    and ??.status = 'NOTTAKEN'
    and ??.from <= date_part(w, current_date - interval '6 weeks') 
    and ??.to   >= date_part(w, current_date)
group by ??.from

请注意,我修改了查询以使用 table 别名。我还建议在每一列前加上它所属的 table 前缀,这样查询就可以清楚地了解底层数据结构:我没有任何线索,所以我使用了 ??,你需要将其替换为dhn.

我对left join也很怀疑。为什么您实际上不想要 inner join 有充分的理由吗?