要求是按日期获取不同且最新的记录。有些记录具有相同的Basket id,但其他信息是唯一的。根据我的方法,我可以获得按照篮子ID分组的desc日期排序的记录,但我不知道如何获得满足我要求的那些排序记录的TOP 1。请建议如何实现这一目标。
ID Flower Country Date
==================================================
A12 Rose China 2016:08:18 16:02:19
A65 Turnip China 2016:08:25 18:25:54
A11 unknown China 2016:08:18 16:45:27
A43 angelica Mexico 2016:08:25 18:25:54
A43 Rose Mexico 2016:08:25 15:46:35
A43 angelica Mexico 2016:08:19 12:33:43
A43 Rose Mexico 2016:08:18 16:23:01
A44 solomon Denmark 2016:08:22 18:49:10
A44 Turnip Denmark 2016:08:18 09:44:06
A49 duetzia Mexico 2016:08:23 21:25:38
A49 Rose Mexico 2016:08:23 18:54:55
A49 angelica Mexico 2016:08:19 12:23:58
A49 Rose Mexico 2016:08:18 16:23:01
B15 solomon Denmark 2016:08:22 18:58:35
B43 solomon Denmark 2016:08:24 20:00:14
B43 Turnip Denmark 2016:08:22 13:57:08
B43 impCalamus Denmark 2016:08:18 17:28:53
B43 impCalamus Denmark 2016:08:18 15:40:45
B45 solomon Denmark 2016:08:24 20:00:14
Expected output
===============
ID Flower Country Date
=============================================
A11 unknown China 2016:08:18 16:45:27
A12 Rose China 2016:08:18 16:02:19
A43 angelica Mexico 2016:08:25 18:25:54
A44 solomon Denmark 2016:08:22 18:49:10
A49 duetzia Mexico 2016:08:23 21:25:38
A65 Turnip China 2016:08:25 18:25:54
B15 solomon Denmark 2016:08:22 18:58:35
B43 solomon Denmark 2016:08:24 20:00:14
B45 solomon Denmark 2016:08:24 20:00:14
Steps to create the Table.
---------------------------
CREATE TABLE FLOWER( ID varchar(12) , FLOWER varchar(12), COUNTRY varchar(12), DATE TIME);
INSERT INTO FLOWER (ID,FLOWER,COUNTRY,DATE) VALUES ('A12', 'Rose', 'China', '2016:08:18 16:02:19' );
INSERT INTO FLOWER (ID,FLOWER,COUNTRY,DATE) VALUES ('A65', 'Turnip' , 'China' , '2016:08:25 18:25:54' );
INSERT INTO FLOWER (ID,FLOWER,COUNTRY,DATE) VALUES ('A11', 'unknown' , 'China' , '2016:08:18 16:45:27' );
INSERT INTO FLOWER (ID,FLOWER,COUNTRY,DATE) VALUES ('A43', 'angelica' , 'Mexico' , '2016:08:25 18:25:54' );
INSERT INTO FLOWER (ID,FLOWER,COUNTRY,DATE) VALUES ('A43', 'Rose' , 'Mexico' , '2016:08:25 15:46:35' );
INSERT INTO FLOWER (ID,FLOWER,COUNTRY,DATE) VALUES ('A43', 'angelica' , 'Mexico' , '2016:08:19 12:33:43' );
INSERT INTO FLOWER (ID,FLOWER,COUNTRY,DATE) VALUES ('A43', 'Rose' , 'Mexico' , '2016:08:18 16:23:01' );
INSERT INTO FLOWER (ID,FLOWER,COUNTRY,DATE) VALUES ('A44', 'solomon' , 'Denmark' , '2016:08:22 18:49:10' );
INSERT INTO FLOWER (ID,FLOWER,COUNTRY,DATE) VALUES ('A44', 'Turnip' , 'Denmark' , '2016:08:18 09:44:06' );
INSERT INTO FLOWER (ID,FLOWER,COUNTRY,DATE) VALUES ('A49', 'duetzia' , 'Mexico' , '2016:08:23 21:25:38' );
INSERT INTO FLOWER (ID,FLOWER,COUNTRY,DATE) VALUES ('A49', 'Rose' , 'Mexico' , '2016:08:23 18:54:55' );
INSERT INTO FLOWER (ID,FLOWER,COUNTRY,DATE) VALUES ('A49', 'angelica' , 'Mexico' , '2016:08:19 12:23:58' );
INSERT INTO FLOWER (ID,FLOWER,COUNTRY,DATE) VALUES ('A49', 'Rose' , 'Mexico' , '2016:08:18 16:23:01' );
INSERT INTO FLOWER (ID,FLOWER,COUNTRY,DATE) VALUES ('B15', 'solomon' , 'Denmark' , '2016:08:22 18:58:35' );
INSERT INTO FLOWER (ID,FLOWER,COUNTRY,DATE) VALUES ('B43', 'solomon' , 'Denmark' , '2016:08:24 20:00:14' );
INSERT INTO FLOWER (ID,FLOWER,COUNTRY,DATE) VALUES ('B43', 'Turnip' , 'Denmark' , '2016:08:22 13:57:08' );
INSERT INTO FLOWER (ID,FLOWER,COUNTRY,DATE) VALUES ('B43', 'impCalamus' , 'Denmark' , '2016:08:18 17:28:53' );
INSERT INTO FLOWER (ID,FLOWER,COUNTRY,DATE) VALUES ('B43', 'impCalamus' , 'Denmark' , '2016:08:18 15:40:45' );
INSERT INTO FLOWER (ID,FLOWER,COUNTRY,DATE) VALUES ('B45', 'solomon' , 'Denmark' , '2016:08:24 20:00:14' );
My Approach
===========
select * from flower group by id, date order by id ,date desc;
ID FLOWER COUNTRY DATE
---------- ---------- ---------- -------------------
A11 unknown China 2016:08:18 16:45:27 <---
A12 Rose China 2016:08:18 16:02:19 <---
A43 angelica Mexico 2016:08:25 18:25:54 <---
A43 Rose Mexico 2016:08:25 15:46:35
A43 angelica Mexico 2016:08:19 12:33:43
A43 Rose Mexico 2016:08:18 16:23:01
A44 solomon Denmark 2016:08:22 18:49:10 <---
A44 Turnip Denmark 2016:08:18 09:44:06
A49 duetzia Mexico 2016:08:23 21:25:38 <---
A49 Rose Mexico 2016:08:23 18:54:55
A49 angelica Mexico 2016:08:19 12:23:58
A49 Rose Mexico 2016:08:18 16:23:01
A65 Turnip China 2016:08:25 18:25:54 <---
B15 solomon Denmark 2016:08:22 18:58:35 <---
B43 solomon Denmark 2016:08:24 20:00:14 <---
B43 Turnip Denmark 2016:08:22 13:57:08
B43 impCalamus Denmark 2016:08:18 17:28:53
B43 impCalamus Denmark 2016:08:18 15:40:45
B45 solomon Denmark 2016:08:24 20:00:14 <---
标有箭头&lt; ----的那些是我感兴趣的人。
答案 0 :(得分:1)
您可以使用where
子句和相关子查询来执行此操作:
select t.*
from t
where t.time = (select max(t2.time)
from t t2
where t2.id = t.id
);