SQL Server extracts the first few entries in each group after grouping

Source: Internet
Author: User
Table: T
Field: Id auto increment,
Field1 number,
.....
Select * From (
Select * ,( Select Count ( * ) From T As T2 Where T2.field1 = T1.field1 And T2.id <= T1.id) As Aaa From T As T1) As T3
Where Aaa <= 2

T2.field1 = t1.field1 is a condition for grouping and can be freely added.

Reference address:

Http://topic.csdn.net/t/20040819/17/3291042.html

 

 

Extract the first five data entries of each category.

 

Select ID, title, d_picture, addtime, newstypeid, JJ, content
From News NS
Where ID In ( Select Top 5 ID From News Where Newstypeid = NS. newstypeid And Isactive = ' 1 ' Order By Addtime Desc )

 

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.