Redshift rank() window 函数基于不同的维度值和时间戳排序
Redshift rank() window function based on distinct dimension values and timestamp ordering
我正在尝试应用 rank() window 函数来捕捉边缘情况。
所以我有 CTE:
with customer_details_type as
(
select cd.*, cp.type
from db.ustomer_details as cd
left join db.customer_private as cp on cp.id = cd.customer_id
)
我想在上面的 CTE 中添加一个 rank(),它将只对具有不同 customer_type 的行进行排名,而不是对具有相同的行进行排名。所以在下面的数据中我想要这样的东西:
我尝试了以下方法:
with customer_details_type as
(
select cd.*, cp.type,
, rank() over (partition by cd.order_id,cd.customer_type order by cd.created desc nulls last) as rank
from db.ustomer_details as cd
left join db.customer_private as cp on cp.id = cd.customer_id
)
但是它按照我感兴趣的排名顺序分配了 rank=1。
所以我要解决的问题是:对于具有 count(distinct customer_type)> 的行,如何在 order_id 分区上使用 rank() window 函数仅 1,按创建的时间戳排序?
上述设置中的 rank() 可以做到这一点,还是我需要为该任务添加第二个 CTE?
我认为你想要:
with customer_details_type as (
select cd.*, cp.type,
dense_rank() over (partition by cd.order_id order by cd.customer_type) as rank
from db.ustomer_details cd left join
db.customer_private cp
on cp.id = cd.customer_id
)
这为每个订单列举了不同的客户类型。
如果您确实希望它们按第一次出现的顺序排列,则计算每种类型的最小值 created
并将其用于排序:
with customer_details_type as (
select cd.*,
dense_rank() over (partition by cd.order_id order by type_created, customer_type) as rank
from (select cd.*, cp.type,
min(created) over (partition by cd.order_id, cd.customer_type) as type_created
from db.customer_details cd left join
db.customer_private cp
on cp.id = cd.customer_id
) cd
)
我正在尝试应用 rank() window 函数来捕捉边缘情况。
所以我有 CTE:
with customer_details_type as
(
select cd.*, cp.type
from db.ustomer_details as cd
left join db.customer_private as cp on cp.id = cd.customer_id
)
我想在上面的 CTE 中添加一个 rank(),它将只对具有不同 customer_type 的行进行排名,而不是对具有相同的行进行排名。所以在下面的数据中我想要这样的东西:
我尝试了以下方法:
with customer_details_type as
(
select cd.*, cp.type,
, rank() over (partition by cd.order_id,cd.customer_type order by cd.created desc nulls last) as rank
from db.ustomer_details as cd
left join db.customer_private as cp on cp.id = cd.customer_id
)
但是它按照我感兴趣的排名顺序分配了 rank=1。
所以我要解决的问题是:对于具有 count(distinct customer_type)> 的行,如何在 order_id 分区上使用 rank() window 函数仅 1,按创建的时间戳排序? 上述设置中的 rank() 可以做到这一点,还是我需要为该任务添加第二个 CTE?
我认为你想要:
with customer_details_type as (
select cd.*, cp.type,
dense_rank() over (partition by cd.order_id order by cd.customer_type) as rank
from db.ustomer_details cd left join
db.customer_private cp
on cp.id = cd.customer_id
)
这为每个订单列举了不同的客户类型。
如果您确实希望它们按第一次出现的顺序排列,则计算每种类型的最小值 created
并将其用于排序:
with customer_details_type as (
select cd.*,
dense_rank() over (partition by cd.order_id order by type_created, customer_type) as rank
from (select cd.*, cp.type,
min(created) over (partition by cd.order_id, cd.customer_type) as type_created
from db.customer_details cd left join
db.customer_private cp
on cp.id = cd.customer_id
) cd
)