我有一个表,其中包含以下示例数据的字段(id,日期,产品)
id date Product current_Flag Expected_flag
14834 2019-01-03 00:00:00 A 1 1
14834 2019-01-31 00:00:00 B 0 0
14834 2019-02-28 00:00:00 C 0 0
14834 2019-03-30 00:00:00 C 0 0
14834 2019-01-03 00:00:00 D 0 1
我正在尝试case when row_number() over (partition by id order by date) = 1 then 1 else 0 end as flag
,
但是对于带有最小日期的特定ID,它将标记标记为1,但是我需要将特定ID和最小日期和乘积标记为1标记,如Expected_flag
case when row_number() over (partition by id order by date) = 1 then 1 else 0 end as flag
答案 0 :(得分:2)
您需要使用dense_rank()
来代替:
case when dense_rank() over (partition by id order by date) = 1 then 1 else 0 end as Expected_flag