SQL analysis function over Partition

Source: Internet
Author: User

Set the atable table as follows:
A B C
Aa sx 1000
Aa zh 900
BB, JJ 1100
Bb ZH 500
Bb Gg 400
Bb WD 1, 300
Bb Sh 1, 150
CC sh 1500

Convert to a btable as follows:
A B C
Aa sx 1000
Aa zh 900
BB, JJ 1100
Bb ZH 500
Bb Gg 400
CC sh 1500

That is, less than three pieces of data are taken out, more than three pieces of data are taken, and three pieces are filtered back.

 

Select T. * From
( Select Atable. * , Row_number () Over (Partition By A Order By C Desc ) Rn From Atable) T
Where Rn <= 3

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.