Sql 配置单元-获取没有空白列的最新记录

Sql 配置单元-获取没有空白列的最新记录,sql,hive,hiveql,Sql,Hive,Hiveql,我有两张桌子: 表1: 我的预期产出是: 如果最新记录有emp_log_id,则取该记录(示例emp_id:2 如果最新的记录没有emp_log_id,则返回到先前更新的记录(示例emp_id:1) 如何为此编写配置单元查询 另一个表包含如下数据: 表2: 如何实现表2中的相同要求。 请提供帮助。使用聚合函数max()即可 select emp_id, max(emp_log_id) as emp_log_id, timestamp from table1 group by emp_id, t

我有两张桌子: 表1:

我的预期产出是:

  • 如果最新记录有emp_log_id,则取该记录(示例emp_id:2
  • 如果最新的记录没有emp_log_id,则返回到先前更新的记录(示例emp_id:1)
  • 如何为此编写配置单元查询

    另一个表包含如下数据: 表2:

    如何实现表2中的相同要求。


    请提供帮助。

    使用聚合函数
    max()
    即可

    select emp_id, max(emp_log_id) as emp_log_id, timestamp 
    from table1
    group by emp_id, timestamp
    

    使用聚合函数
    max()

    select emp_id, max(emp_log_id) as emp_log_id, timestamp 
    from table1
    group by emp_id, timestamp
    

    我建议
    行编号()


    我建议
    行编号()


    根据你的描述,@Gordon Linoff的答案应该更好。被接受的答案并不保证
    emp\u log\u id
    timestamp
    来自同一记录。你能不能用更多的细节来编辑你的帖子?你能不能用更多的细节来编辑你的帖子l根据您的描述,@Gordon Linoff给出的答案应该更好。接受的答案不能保证
    emp\u log\u id
    timestamp
    来自同一条记录。
    emp_id emp_log_id
        1 234
        1  05
        2 335
        2  03
        3  04
        4 324
    
    select emp_id, max(emp_log_id) as emp_log_id, timestamp 
    from table1
    group by emp_id, timestamp
    
    select t.*
    from (select t.*,
                 row_number() over (partition by emp_id
                                    order by (emp_log_id is not null) desc, timestamp desc
                                   ) as seqnum
          from t
         ) t
    where seqnum = 1