About SQL statements Group and then sort and then take the first piece of data for each group

Source: Internet
Author: User

Select ranked. Wait Time from
(
SELECT *, Row_number () over (partition by TB. PATIENT_ID ORDER by TB. Wait time ASC) as RowNum
From (select a.patient_id as patient_id, DATEDIFF (Hour,b.admission_date, a.oper_start_date) as wait time
From Pat_operation as a,pat_in_visit as B
where operation_code= ' Setmi_op01 ' and a.patient_id=b.patient_id and discharge_date>= ' 2014-04-01 '
and discharge_date< ' 2014-07-01 ') TB
) ranked
where Ranked.rownum <=1

Because each ID corresponds to two wait times I take the first one

About SQL statements Group and then sort and then take the first piece of data for each group

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.