根据排名获得潜在客户价值

get lead value based on rankings

我正在尝试根据排名获取潜在客户价值。 示例:对于排名#1(使用排名window 函数),我需要排名#2 的特定列的值。我的问题是,如果排名#1 有平局(比如 2 行在排名#1 并列),我为行#1 获得的先验值是正确的(排名#3 的列值),而行上的先验值#2 变空了。

sample data
rankings, employee, col_date,     job, col_a, col_a_date
1        john smith  2022-05-12   a    abc    2022-05-12 
1        john smith  2022-05-12   b    def    2022-05-12 
3        john smith  2022-03-27   a    ghi    2022-03-27 
4        john smith  2021-09-21   a    abc    2021-09-21

What I'm getting - actual result
rankings, employee, col_date,     job, col_a, col_a_date, prev_col_a
1        john smith  2022-05-12   a    abc    2022-05-12  ghi
1        john smith  2022-05-12   b    def    2022-05-12  def
3        john smith  2022-03-27   a    ghi    2022-03-27  abc
4        john smith  2021-09-21   a    abc    2021-09-21  <null>


Expected result
rankings, employee, col_date,     job, col_a, col_a_date, prev_col_a
1        john smith  2022-05-12   a    abc    2022-05-12  ghi
1        john smith  2022-05-12   b    def    2022-05-12  ghi
3        john smith  2022-03-27   a    ghi    2022-03-27  abc
4        john smith  2021-09-21   a    abc    2021-09-21  <null>

对于两个排名#1,我想获得下一个排名即排名#3 的 col_a 的值。

select *, lead(col_a,1) OVER w as prev_col_a
from (select RANK() OVER (PARTITION BY  employee ORDER BY col_a_date DESC) rankings,
      *
      from table
) WINDOW w AS (
        PARTITION BY employee, job
        ORDER BY col_a_date DESC
    )
  order by rankings, col_a_date desc

您的样本太小,无法验证查询逻辑。根据实际数据,以下查询可能无法按预期工作。但希望这对您解决问题有所帮助。

-- Sample Table
CREATE TEMP TABLE sample (
  employee STRING,
  col_date STRING,
  job STRING,
  col_a STRING,
  col_a_date STRING,
) AS
SELECT * FROM UNNEST([
 ('john smith', '2022-05-12', 'a', 'abc', '2022-05-12'),
 ('john smith', '2022-05-12', 'b', 'def', '2022-05-12'),
 ('john smith', '2022-03-27', 'a', 'ghi', '2022-03-27'),
 ('john smith', '2021-09-21', 'a', 'abc', '2021-09-21')
]);

-- Main Query
SELECT * EXCEPT(dense_rankings), 
       IF(rankings = MAX(rankings) OVER (PARTITION BY employee), NULL, LAST_VALUE(col_a) OVER w) AS prev_col_a 
  FROM (
    SELECT RANK() OVER w rankings, DENSE_RANK() OVER w dense_rankings, *
      FROM sample
    WINDOW w AS (PARTITION BY employee ORDER BY col_a_date DESC)
  )
WINDOW w AS (PARTITION BY employee ORDER BY dense_rankings RANGE BETWEEN CURRENT ROW AND 1 FOLLOWING)
 ORDER BY rankings, col_a_date DESC
;

输出: