Windows功能过滤子句在RedShift中

发布于 2025-02-12 06:14:54 字数 1915 浏览 0 评论 0原文

我有一个红移的下表。我正在尝试在此处描述的Windows函数中实现一个过滤子句 - 在窗口函数的过滤器子句中引用当前行但是过滤器子句在红移中不起作用,

在redshift中进行查询:

select date, super_location , super_location, location, condition, 
,count(location) FILTER (where condition = 1) 
 over (partition by super_location,super_location order by date) as count
from table1 

示例数据:

date       | super_location | sub_super_location| location| condition
-----------|---------------|-------------------| -------- | ----------
2022-06-28 | AAA           | AA                | A1       | 1

2022-06-27 | AAA           | AA                | A1       | 0

2022-06-26 | AAA           | AA                | A1       | 0

2022-06-28 | AAA           | AA                | A2       | 1

2022-06-27 | AAA           | AA                | A2       | 1

i想要计算条件= 1的位置,并按超级位置,子超级位置和订单_DATE进行分区。在每一行中应可见计数。因此,由此产生的数据集应该看起来像这样 -

结果数据集:

Order_date | super_location | sub_super_location | location | condition |count
-----------|----------------|------------------- | -------- | ----------|-----
2022-06-28 | AAA1           | AA1                | A1       | 1         |2 (counting A1 and A2 on 06/28 where condition = 1)

2022-06-27 | AAA1           | AA1                | A1       | 0         |1 (counting A2 on 06/27 where where condition = 1)

2022-06-26 | AAA1           | AA1                | A1       | 0         |0 (no location with condition = 1 on 06/26)

2022-06-28 | AAA1           | AA1                | A2       | 1         |2 (counting A1 and A2 on 06/28 where condition = 1)

2022-06-27 | AAA1           | AA1                | A1       | 1         |1 (counting A1 on 06/27 where condition = 1)

I have following table in a Redshift. I am trying to implement a filter clause in windows function that is described over here - Referencing current row in FILTER clause of window function but filter clause does not work in Redshift

Trying to run following query in Redshift:

select date, super_location , super_location, location, condition, 
,count(location) FILTER (where condition = 1) 
 over (partition by super_location,super_location order by date) as count
from table1 

Sample Data:

date       | super_location | sub_super_location| location| condition
-----------|---------------|-------------------| -------- | ----------
2022-06-28 | AAA           | AA                | A1       | 1

2022-06-27 | AAA           | AA                | A1       | 0

2022-06-26 | AAA           | AA                | A1       | 0

2022-06-28 | AAA           | AA                | A2       | 1

2022-06-27 | AAA           | AA                | A2       | 1

I want to count locations where condition = 1 and partition by super location, sub super location and order_date. The count should be visible in every row. So the resulting dataset should look like this -

Resulting Dataset:

Order_date | super_location | sub_super_location | location | condition |count
-----------|----------------|------------------- | -------- | ----------|-----
2022-06-28 | AAA1           | AA1                | A1       | 1         |2 (counting A1 and A2 on 06/28 where condition = 1)

2022-06-27 | AAA1           | AA1                | A1       | 0         |1 (counting A2 on 06/27 where where condition = 1)

2022-06-26 | AAA1           | AA1                | A1       | 0         |0 (no location with condition = 1 on 06/26)

2022-06-28 | AAA1           | AA1                | A2       | 1         |2 (counting A1 and A2 on 06/28 where condition = 1)

2022-06-27 | AAA1           | AA1                | A1       | 1         |1 (counting A1 on 06/27 where condition = 1)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文