如何在红移中获取过去 X 周的数据?

Posted

技术标签:

【中文标题】如何在红移中获取过去 X 周的数据?【英文标题】:How to get data for past X weeks in redshift? 【发布时间】:2020-11-06 15:10:52 【问题描述】:

我有一个下面的查询,我运行它给了我上周的单次计数,即Week 44。截至目前,本周是 45 周。

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
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 (fromWeek <= '44' AND toWeek >= '45')

截至目前,我看到的输出是第 44 周的输出-

Count
-----
124 

现在我正在尝试使这个查询动态化,以便它可以让我计算过去 6 周从 44 到 39 不包括当前周的输出,如下所示:

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

所以如果我每周手动运行它,我每周的情况都会是这样 -

and (fromWeek <= '44' AND toWeek >= '45') // for week 44
and (fromWeek <= '43' AND toWeek >= '44') // for week 43
and (fromWeek <= '42' AND toWeek >= '43') // for week 42
and (fromWeek <= '41' AND toWeek >= '42') // for week 41
and (fromWeek <= '40' AND toWeek >= '41') // for week 40
and (fromWeek <= '39' AND toWeek >= '40') // for week 39

现在我需要以动态的方式每周进行上述手动查询。我想出了下面的查询,但不知何故,当我运行它时我没有得到任何输出。我在下面的查询中做错了什么吗?

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 fromWeek,
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 fromWeek <= date_part(w, current_date - interval '6 weeks') 
and toWeek >= date_part(w, current_date)
group by fromWeek

注意:

fromWeektoWeek 列是整数数据类型,它们包含周数仅供参考。这是一个遗留查询,所以我试图让它动态化。不知道为什么他们在 44 和 45 左右有单引号,但如果我在第 44 周运行像 and (fromWeek &lt;= '44' AND toWeek &gt;= '45')and (fromWeek &lt;= 44 AND toWeek &gt;= 45) 这样的原始手动查询,我总是得到与 124 相同的数据。

【问题讨论】:

你得到什么输出? 正如我上面提到的,它没有给我任何回报。我在动态查询中做错了吗? fromWeektoWeek 是如何定义的?这个表达我看不懂(fromWeek &lt;= '44' AND toWeek &gt;= '45') 我更新了我的问题。它们是整数数据类型,因为它是一个遗留查询,所以试图使其动态化。不知道为什么他们在 44 和 45 左右有单引号,但是如果我在第 44 周运行像 and (fromWeek &lt;= '44' AND toWeek &gt;= '45')and (fromWeek &lt;= 44 AND toWeek &gt;= 45) 这样的原始手动查询,我总是得到与 124 相同的数据。 我不明白这两列是如何协同工作的,fromWeek &lt;= '44' 表示 fromWeek 可以是 0 到 44 之间的任何值,AND toWeek &gt;= 45 表示它可以是大于或等于 45 的任何值。为什么会这样准确定义第 44 周? 【参考方案1】:

您不想让 toWeek 比 fromWeek 大 1 吗?如果是这样,您的 where 子句应该是:

...
and fromWeek <= date_part(w, current_date - interval '6 weeks') 
and toWeek >= date_part(w, current_date - interval '5 weeks')
...

我怀疑您只是没有足够高的 toWeek 值数据来传递您的 toWeek where 子句。

【讨论】:

以上是关于如何在红移中获取过去 X 周的数据?的主要内容,如果未能解决你的问题,请参考以下文章

数据值“0”在红移中有无效格式错误

在红移中插入

在红移中评估的顺序是啥

在红移中具有 DISTINCT 的 listagg

在红移中作为选择插入

反正有没有在红移表中只保留一周的数据