我在 bigquery 中使用 #standardsql
并尝试将每个 customer_id 的最大排名编码为 1
,其余为 0
这是目前的查询结果
排名查询是这个
ROW_NUMBER() OVER(PARTITION BY customer_id ORDER BY booking_date Asc) as ranking
我需要创建另一个像这样的列,它将每个 customerid
的最大排名解码为 1,将其下方的数字解码为 0
,如下所示表格
谢谢
最佳答案
根据您的示例数据,您的排名不稳定,因为您有多行具有相同的键值。在任何情况下,您仍然可以在没有子查询的情况下执行您想要的操作,只需使用 case
:
select t.*,
row_number() over (partition by customer_id order by booking_date asc) as ranking,
(case when row_number() over (partition by customer_id order by booking_date asc) =
count(*) over (partition by customer_id)
then 1 else 0
end) as custom_coded
from t;
做基本相同事情的更传统的方法是使用降序:
select t.*,
row_number() over (partition by customer_id order by booking_date asc) as ranking,
(case when row_number() over (partition by customer_id order by booking_date desc) = 1
then 1 else 0
end) as custom_coded
from t;
关于sql - 解码sql的最大行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47530329/