【问题标题】:SemanticException[Error 10004] Invalid table alias or column referenceSemanticException[Error 10004] 无效的表别名或列引用
【发布时间】:2020-04-24 10:54:39
【问题描述】:

我正在尝试按 id 获取每个组中第一行的记录。行号由id的顺序生成。我一直收到同样的错误[10004]。

这个hive脚本是在SAS studio环境下构建的,下面是一段proc sql代码:

数据集 id_f 包含变量 x,y 和 z

execute (create temporary table event_id_f1 as
        select *,ROW_NUMBER() OVER(ORDER BY id) as rownum
        from id_f
                        group by id
                        having rownum = min(rownum)
        )
by df20;

错误:执行错误:org.apache.hive.service.cli.HiveSQLException:编译语句时出错:FAILED:SemanticException [错误 10004]:第 1:129 行无效的表别名或列引用“rownum”:(可能的列名称为:x、y、z)

感谢任何帮助!

@bernie 建议 Hive 不识别别名。我应该如何嵌套别名以使其工作?代码也在执行语句中。 我的尝试:

execute (create temporary table event_id_f1 as

        from (
             select *,
             ROW_NUMBER() OVER(ORDER BY id) as rownum
             from id_f
              group by id
              having rownum = min(rownum) ) ranked
             WHERE ranked=1
             )
by df20;

【问题讨论】:

  • @bernie 你知道如何将我当前的代码转换为嵌套选择的行号而不是别名吗?我试图转换它,不确定它是否正确。我第一次在 Hive 中编码。您可以刷新帖子以查看我的尝试。
  • @bernie 刚刚做了一些修改..
  • 我只是发布了我写它的方式。告诉我系统是否返回任何其他错误。
  • @bernie 我明天就会知道,而不是通过工作计算机。非常感谢!

标签: sql hive sas


【解决方案1】:

这就是我写它的方式。如果有任何错误,请告诉我:

execute (create temporary table event_id_f1 as
    select * 
    from (
        select id, ROW_NUMBER() OVER (ORDER BY id) as rownum
        from id_f
        group by id 
    ) ranked
    where ranked.rownum = 1         
) by df20;

【讨论】:

    【解决方案2】:

    我的拙见是以下SQL有问题(取自另一个答案):

    select * 
    from (
        select id, ROW_NUMBER() OVER (ORDER BY id) as rownum
        from id_f
        group by id 
    ) ranked
    where ranked.rownum = 1
    

    这个查询分为三个阶段:

    首先,select id from id_f group by id,相当于得到所有不同的id

    然后,对order by 这些不同的 ID 进行全局窗口化,并为每个 ID 分配一个行号。

    最后,where ranked.rownum = 1 过滤器使结果集只包含一行:

    minimal_id, 1
    

    如果这就是你想要的,使用min 应该会更好。

    乍一看,这个问题对我来说没有多大意义。我猜你要找的是partition by id,在每个分区内order by something_else,然后选择在这些不同分区中排名第一的行。如果幸运的是我的猜测是正确的,我会建议这样:

    select 
        * 
    from (
        select 
            *, ROW_NUMBER() OVER (partition by id ORDER BY STH_ELSE) as rownum
        from id_f
    ) ranked
    where ranked.rownum = 1
    

    【讨论】:

    • 使用 min 是什么意思?你的意思是我应该使用 rownum = min(rownum) 吗?而不是在最后使用 where ?
    • 哦,我认为只提取一个最小值是不够的,我想要的是提取每个 ID 组中的最小行号。因此,每个不同的 ID 会有一行。 @gaccio
    • 我猜这个订单是不必要的,因为它在同一个 ID 内?我认为很困惑首先认为我必须按 ID 为行号分配顺序。
    • @lydias 我提供的 SQL 用于为每个不同的 ID 获取一行。如果您不关心这些行在每个 ID 分区中的排序方式,那么您是对的,您可能不需要 order by 部分。虽然,消除order by 并没有明显的效率提升,但无论哪种情况,都会启动一个 MR 作业。
    • 这些提示是否解决了您的问题?如果需要更多信息,请告诉我。
    猜你喜欢
    • 2021-09-16
    • 2019-07-12
    • 1970-01-01
    • 2018-09-18
    • 1970-01-01
    • 2023-03-30
    • 2020-08-23
    • 1970-01-01
    • 2016-06-07
    相关资源
    最近更新 更多