如何按日期获取最新的行?

时间:2015-11-10 01:54:31

标签: sql sql-server select sql-order-by

我有一个显示如下数据的现有查询:

NAME     COURSE   PROF    LASTUPDATE
NAME_1   MATH1    PROF_A  2015-10-14 09:55:26.637
NAME_2   ENG1     PROF_B  2015-10-14 09:54:07.087
NAME_2   ENG1     PROF_C  2015-10-14 10:02:31.663 
NAME_3   SOC1     PROF_D  2015-10-14 09:53:28.253 
NAME_4   PHL1     PROF_E  2015-10-14 09:55:47.293
NAME_3   SOC1     PROF_F  2015-10-14 09:59:00.000

我想只选择具有相同名称和课程的最新行(就lastupdate而言)?结果表应如下所示:

NAME     COURSE   PROF    LASTUPDATE
NAME_1   MATH1    PROF_A  2015-10-14 09:55:26.637
NAME_2   ENG1     PROF_C  2015-10-14 10:02:31.663  
NAME_4   PHL1     PROF_E  2015-10-14 09:55:47.293
NAME_3   SOC1     PROF_F  2015-10-14 09:59:00.000

提前谢谢!

2 个答案:

答案 0 :(得分:1)

一种典型的方法是<jdbc-connection-pool datasource-classname="oracle.jdbc.pool.OracleConnectionPoolDataSource" name="MyPoolName" res-type="javax.sql.ConnectionPoolDataSource"> <property name="User" value="UserName"></property> <property name="URL" value="jdbc:oracle:thin:@//localhost:1521/xe"></property> <property name="Password" value="UserPassword"></property> </jdbc-connection-pool>

row_number()

答案 1 :(得分:0)

尝试first_value

select
first_value(last_update) over (partition by name_2 order by last_update DESC) as last_update 
from my_table;