SQL one-to-multiple-fetch child table all fields of the latest record (Row_number () over () function application)

Source: Internet
Author: User

Row_number () over ()

Parameter 1: grouping fields PARTITION by ............ Options available

Parameter 2: Sort field ORDER BY: DESC must

Instance:

Sort by confirmdate, Payapplyid Group

SELECT * from Payapplyconfirmrecord
SELECT row_number () over (PARTITION by Payapplyid ORDER by Confirmdate DESC) rowid,* from dbo. Payapplyconfirmrecord

Results:

Practical Application Scenarios

Payment Application Form Payapply Association Audit table Payapplyconfirmrecord relationship is one-to-many, query all payment application records and the latest audit records

SELECT b.*,a.* from dbo. Payapply A
The Left JOIN (SELECT * FROM (select Row_number () over (PARTITION by Payapplyid ORDER BY confirmdate Desc) is rowid,* from dbo. Payapplyconfirmrecord) a where a.rowid=1) b on A.id=b.payapplyid

Results:

Of course, this function can also be used for deduplication and paging, which is just a data I have encountered in the development of the actual need to query

SQL one-to-multiple-fetch child table all fields of the latest record (Row_number () over () function application)

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.