SQL Server group take top N Records

Source: Internet
Author: User

1. Preface.

There are two ways of doing this. 1 is used in,2 is used row_number (), can be used according to the situation. Both of them are OK.

2. For example.

Method 1:

Java code

Select A.* from table1 a where a.column1 in (select Max (column1) to Table1 Group by A.column2,column3)

Method 2:

Java code

With Nyr as (

Select Nyr,qyzx_dm,fxsp_xh,spbm_dlj,wbsp_gg,jglx,jzjg,max (SJGX_SJ),

Row_number () over (partition by Nyr ORDER by Nyr) as ' Rowno '

From Wd_o_t_ic_spsj_fx

where rowno=1

GROUP BY NYR,QYZX_DM,FXSP_XH,SPBM_DLJ,WBSP_GG,JGLX,JZJG

Order by Nyr,qyzx_dm,fxsp_xh,spbm_dlj,wbsp_gg,jglx,max (SJGX_SJ) desc)

SELECT *

From Nyr

where Rowno = 1

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.