sql - 选择最新记录
问题描述
我想运行一个 select 语句,该语句运行并仅通过 Recored_timestampe 字段为键 teacher_id 和 student_id 选择最新记录。所以任何时候,它运行时只需要提供一条记录。请问我该怎么做?输出可能没有字段 Recored_timestamp。谢谢
解决方案
使用窗口函数,按teacher_id 和student_id 分区并按recorded_timestamp 对其进行排序将为您提供所需的结果。
select * from(select teacher_id,student_id,teacher_name,comment ,recorded_timestamp, row_number() over(partition by teacher_id,student_id order by recorded_timestamp desc)as rownum from temp0607)out1 where rownum=1
此外,您可能需要查看记录的时间戳的存储方式。如果它存储为字符串,您可以使用将其转换为时间戳from_unixtime(unix_timestamp(recorded_timestamp,'dd/MM/yyyy HH:mm'),'dd/MM/yyyy HH:mm')
推荐阅读
- javascript - 空 Promise.resole() 的返回类型 new Promise Typescript
- scip - SCIP:如何确定现有解决方案中变量的当前值是否为整数?
- python - 按钮在 Tkinter Python 应用程序中不起作用
- ramda.js - Ramda - 从一个对象中挑选并返回一个数组
- constraint-programming - Minizinc 模型在类似于 Vehicle Route Problem 的问题中的拾取和交付
- reactjs - 如何将 JSON 从 materialTopTabNavigator 传递到其他屏幕组件反应原生?
- python - PYTHON-DOTENV 错误:没有名为“dotenv”的模块
- python - Python中的Selenium:关闭警报不起作用
- c++ - 如何在两个 boost::multi_arrays (C++) 之间执行数学运算?
- r - For 循环和计数函数 R