每天选择最后的数据

时间:2017-08-11 07:01:08

标签: sql database oracle

我有一张表CAD_CHANGES

ID           OBJECT              U_ID                   CH_DATE

1            parcel               20               2017-08-02 15:25:56
2            parcel               20               2017-08-02 10:31:50
3            parcel               20               2017-08-02 18:21:45
4            building             20               2017-08-02 14:03:56
5            building             20               2017-08-02 12:10:10
6            parcel               20               2017-08-02 20:21:56
7            building             20               2017-08-02 21:05:05 
8            parcel               20               2017-08-02 09:14:27
9            parcel               20               2017-08-02 12:08:15
10           building             20               2017-08-03 14:09:26
11           building             20               2017-08-03 11:08:37
12           building             20               2017-08-03 18:01:48
13           building             20               2017-08-03 19:05:59
14           building             20               2017-08-03 21:28:02
15           building             20               2017-08-03 23:31:05
16           parcel               20               2017-08-03 09:34:07 
17           parcel               20               2017-08-03 20:21:08
18           parcel               20               2017-08-03 15:42:04
19           building             20               2017-08-03 10:51:37
20           parcel               20               2017-08-03 14:10:22

我想以结果为例

ID               OBJECT              U_ID                   CH_DATE
--------------------------------------------------------------------------
3                parcel               20              2017-08-02 18:21:45
7               building              20              2017-08-02 21:05:05
---------------------------------------------------------------------------
15              building              20              2017-08-03 23:31:05
17               parcel               20              2017-08-03 20:21:08

我的桌子很大,我只写了两天,请帮忙解决问题, 提前谢谢你

(aaa bbb ccc ddd eee fff)

1 个答案:

答案 0 :(得分:1)

像这样的例子

SELECT * FROM CAD_CHANGES WHERE ( OBJECT, CH_DATE ) IN ( 
SELECT OBJECT, MAX(CH_DATE) FROM CAD_CHANGES GROUP BY OBJECT, TRUNC(CH_DATE))

或者在每个字段上使用像last_value这样的分析函数,但这样写起来会有点痛苦