Oracle group obtains the first data entry

Source: Internet
Author: User

 

Group and sort multiple data and obtain the first record in each group.

 

SQL code

SELECT *

FROM (SELECT ROW_NUMBER () OVER (partition by <PK_FINANCE_ACCOUNT_1> order by <amassdate> DESC) lev,

<Hx_amass_liquid> .*

FROM

WHERE lev_= 1

Order by <PK_FINANCE_ACCOUNT_2> DESC

 

SELECT *

FROM (SELECT ROW_NUMBER () OVER (partition by <PK_FINANCE_ACCOUNT_1> order by <amassdate> DESC) lev,

<Hx_amass_liquid> .*

FROM

WHERE lev_= 1

Order by <PK_FINANCE_ACCOUNT_2> DESC

 

<PK_FINANCE_ACCOUNT_1> -- group column

 

 

 

<Amassdate> -- Sort Columns

 

 

 

<Hx_amass_liquid> -- table name

 

 

 

<PK_FINANCE_ACCOUNT_2> -- sort column

 

 

 

Note:

 

 

 

========================================================== ============================

 

 

 

When grouping, you not only want to retrieve the first record of each group, but also want to retrieve several records in each group.

 

 

SQL code

SELECT *

FROM (

 

 

SELECT ROW_NUMBER () OVER (partition by PK_FINANCE_ACCOUNT order by amassdate DESC) lev,

 

Count (0) over (partition by PK_FINANCE_ACCOUNT) cnt,

 

 

Hx_amass_liquid .*

FROM

 

 

Hx_amass_liquid

 

 

)

 

 

WHERE lev_= 1

Order by PK_FINANCE_ACCOUNT DESC

Author: Liu Yafei

Related Article

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.