【问题标题】:ROW_NUMBER( ) OVER in impala黑斑羚中的 ROW_NUMBER( ) OVER
【发布时间】:2014-12-01 03:07:33
【问题描述】:

我有一个用例,我需要在 PARTITION 上使用 ROW_NUMBER(): 比如:

SELECT
  Column1 , Column 2
  ROW_NUMBER() OVER (
    PARTITION BY ACCOUNT_NUM
    ORDER BY FREQ, MAN, MODEL) as LEVEL
FROM
  TEST_TABLE

我需要在 Impala 中解决此问题。不幸的是,Impala 不支持子查询,也不支持 ROW_NUMBER() OVER 功能。 感谢您的帮助。

【问题讨论】:

  • Impala 将同时支持分析窗口函数(包括 ROW_NUMBER())以及即将发布的 2.0 版本中的相关子查询。

标签: sql window-functions impala


【解决方案1】:

Impala 对于这种类型的查询相当有限。有了一些假设,这个查询是可能的:

  • 分区子句中的四列永远不是NULL
  • 分区子句中的四列唯一标识一行

查询相当丑陋和昂贵:

select tt.column1, tt.column2, count(*) as level
from test_table tt join
     test_table tt2
     on tt.account_num = tt2.account_num and
        (tt2.freq < tt.freq or
         tt2.freq = tt.freq and tt2.man < t.man or
         tt2.freq = tt.freq and tt2.man = t.man and tt2.model <= t.model
        )
group by tt.column1, tt.column2, tt.account_num, tt.freq, tt.man, tt.model;

【讨论】:

    【解决方案2】:

    ROW_NUMBER() OVER PARTITION 已在 CDH 5.2 中添加:

    https://www.cloudera.com/documentation/enterprise/latest/topics/impala_analytic_functions.html#row_number

    ROW_NUMBER() OVER([partition_by_clause] order_by_clause)
    

    【讨论】:

      【解决方案3】:

      Impala 现在支持 over 子句。 语法与问题中的相同。

      SELECT
        Column1 , Column 2
        ROW_NUMBER() OVER (
          PARTITION BY ACCOUNT_NUM
          ORDER BY FREQ, MAN, MODEL) as LEVEL
      FROM
        TEST_TABLE
      

      Impala 文档: https://www.cloudera.com/documentation/enterprise/5-6-x/topics/impala_analytic_functions.html#over

      【讨论】:

        【解决方案4】:

        Impala 支持子查询。在括号中和使用with 函数。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-04-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多