Multiple methods for querying the maximum time record by group

Source: Internet
Author: User


Multiple methods for querying the maximum time record in a group: [SQL] create table dispatch_result (dr_id number not null, sheet_id number, check_next varchar (20), check_time date, check_sn char (8), check_comment varchar (255), check_status number, constraint PK_RESULT primary key (dr_id); Sequence Table Record: DR_ID SHEET_ID CHECK_NEXT CHECK_TIME CHECK_SN 1_check_status -------------- skip ---- failed ------------------------------------------------------- ---- -------------------------------------------------------------------------------- ------------ 1 1 10001 2012/8/12 11:11:23 10000 1 2 1 10002 2012/9/15 11:15:24 10001 2 3 2 10001 2012/8/15 9:16:10 10000 1 4 2 10002 2012/9/18 11:16:23 10001 2 query the maximum time record for the SHEET_ID group: method 1: [SQL] select * from dispatch_result dr where dr. check_time = (select max (check_time) from dispatch_result where sheet_id = dr. s Heet_id) DR_ID SHEET_ID CHECK_NEXT CHECK_TIME CHECK_SN CHECK_COMMENT CHECK_STATUS ------------ hour ------------- -------- hour -------------- 2 1 10002 hour 11:15:24 10001 2 4 2 10002 hour 11:16:23 10001 2 www.2cto.com second group by mode: [SQL] select t1. * from dispatch_result t1 right join (select Sheet_id, max (check_time) ct from dispatch_result group by sheet_id) t2 on KERNEL _ id = KERNEL _ id and t2.ct = t1.check _ time DR_ID SHEET_ID CHECK_NEXT CHECK_TIME CHECK_SN CHECK_COMMENT CHECK_STATUS ------------ kernel ----------- -------- kernel ------------ 2 1 10002 bytes 11:15:24 10001 2 4 2 10002 11:16:23 10001 2 or [SQL] select * from dispatch_result inner join (select max (dr. check_time) as check_time, dr. sheet_id from dispatch_result dr group by dr. sheet_id) t1 using (check_time, sheet_id); www.2cto.com CHECK_TIME SHEET_ID DR_ID CHECK_NEXT CHECK_SN CHECK_COMMENT CHECK_STATUS ----------- ---------- -------------------- accept --------------------------------------------------------------------------------------------------------- -------------------------------- ------------ 11:15:24 1 2 10002 10001 22012/9/18 11:16:23 2 4 10002 10001 2 3 partition by method: [SQL] select t2. * from (select t1. *, row_number () over (partition by t1.sheet _ id order by t1.check _ time desc nulls last) rn from dispatch_result t1) t2 where rn = 1 hour DR_ID SHEET_ID CHECK_NEXT CHECK_TIME CHECK_SN CHECK_COMMENT CHECK_STATUS --------------- ----- -------------------- ----------- -------- ------------ 2 1 10002 11:15:24 10001 2 4 2 10002 11:16:23 10001 2 results are the same. For efficiency, please kindly advise. Author hanzhou4519

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.