SQL to re-display the first piece of data to display a single piece of data

Source: Internet
Author: User

Requirement Description: To display any data, first or last, according to a field or several fields.

Data styles such as:

Try to resolve:

-- Count (*) method (only shows the number of bars is 1, more than 1 filter all) Select *  from where inch (Selectfromgroupby hasCount(id)=  1)

The above method will show only 1 records, but the repetition does not keep one of them, also filtered out.

So, we need to change a bit and provide three solutions:

First, by the row_number by the repeating field grouping sorts, then displays 1th, uses the AB table way:

--Select  A.* from T4 A, (Selectthrough  order by as from T4) bwhere=  and B.nid=1


Second, the same method, by Row_number by repeating the field of grouping sorting, and then display 1th. But using sub-query mode:

Select A.*fromselect row_number ()throughorder   by as Nid,* from T4  ) awhere a.nid=  1


Third, by grouping to take the minimum or maximum value for resolution.

Select *  from where inch (Selectminfromgroup by self-coding)

This is the simplest approach, but the ID requirement must be a number that can be calculated for min or max. If there is no pure digital ID, you can use Row_number to establish NID first, then Min.

Select Row_number ()throughorder by as Nid,*from T4


The above three kinds of schemes have the same effect.

SQL to re-display the first data to display a single piece of data

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.