添加尊重每行顺序的行号

时间:2019-06-11 14:59:17

标签: sql vertica

我有一张这样的桌子

id, period, tag
1     1      A
1     2      A
1     3      B
1     4      A
1     5      A
1     6      A
2     1      A
2     2      B
2     3      B
2     4      B
2     5      B
2     6      A

我想添加一个具有排名的新列,并在给定我的列“ period”的情况下尊重行的顺序,以获得类似的内容

id, period, tag  rank
1     1      A     1
1     2      A     1
1     3      B     2
1     4      A     3
1     5      A     3
1     6      A     3
2     1      A     1
2     2      B     2
2     3      B     2
2     4      B     2
2     5      B     2
2     6      A     3

我该怎么办?

我尝试了rank和density_rank函数,但没有成功

2 个答案:

答案 0 :(得分:0)

一种方法是基于lag()的累积总和:

select t.*,
       sum(case when prev_tag = tag then 0 else 1 end) over (partition by id order by period) as rank
from (select t.*, lag(tag) over (partition by id order by period) as prev_tag
      from t
     ) t;

答案 1 :(得分:0)

和另一位CONDITIONAL_CHANGE_EVENT()的候选人 更少的代码,也非常有效...!

WITH
input(id,period,tag) AS (
          SELECT 1,1,'A'
UNION ALL SELECT 1,2,'A'
UNION ALL SELECT 1,3,'B'
UNION ALL SELECT 1,4,'A'
UNION ALL SELECT 1,5,'A'
UNION ALL SELECT 1,6,'A'
UNION ALL SELECT 2,1,'A'
UNION ALL SELECT 2,2,'B'
UNION ALL SELECT 2,3,'B'
UNION ALL SELECT 2,4,'B'
UNION ALL SELECT 2,5,'B'
UNION ALL SELECT 2,6,'A'
)
SELECT
  *
, CONDITIONAL_CHANGE_EVENT(tag) OVER(PARTITION BY id ORDER BY period) + 1 AS rank
FROM input;
-- out  id | period | tag | rank 
-- out ----+--------+-----+------
-- out   1 |      1 | A   |    1
-- out   1 |      2 | A   |    1
-- out   1 |      3 | B   |    2
-- out   1 |      4 | A   |    3
-- out   1 |      5 | A   |    3
-- out   1 |      6 | A   |    3
-- out   2 |      1 | A   |    1
-- out   2 |      2 | B   |    2
-- out   2 |      3 | B   |    2
-- out   2 |      4 | B   |    2
-- out   2 |      5 | B   |    2
-- out   2 |      6 | A   |    3
-- out (12 rows)
-- out 
-- out Time: First fetch (12 rows): 14.823 ms. All rows formatted: 14.874 ms