我有一个表格,如下所示:
Date User Product
11/15/2019 123 NULL
11/21/2019 123 A
11/21/2019 123 A
11/23/2019 123 B
我想运行一个将跳过空值的dense_rank函数。
以下是我目前拥有的:
CASE WHEN PRODUCT IS NOT NULL
THEN DENSE_RANK()
OVER (PARTITION BY USER ORDER BY DATE ASC)
ELSE 1
END DENSE_RANK_OUTPUT
我当前的输出:
Date User Product DENSE_RANK_OUTPUT
11/15/2019 123 NULL 1
11/21/2019 123 A 2
11/21/2019 123 A 2
11/23/2019 123 B 3
我想要的输出是:
Date User Product DESIRED_OUTPUT
11/15/2019 123 NULL 1
11/21/2019 123 A 1
11/21/2019 123 A 1
11/23/2019 123 B 2
你很接近。 只需使用partition by
中的另一个键:
(CASE WHEN PRODUCT IS NOT NULL
THEN DENSE_RANK() OVER (PARTITION BY USER, (PRODUCT IS NOT NULL) ORDER BY DATE ASC)
ELSE 1
END) as DENSE_RANK_OUTPUT