Sorts a field in a database table by deduplication.

Source: Internet
Author: User

Sorts a field in a database table by deduplication.

1. Problem background

In a database, t_tab_ AB has two fields, a and B, as shown below:

Before query: After query:

A B

1 2 1 2

1 3 2 3

1 4 3 4

1 5

2 3

2 4

2 5

3 4

3 5


2. Solution

Groups and sorts a by querying the minimum values in a and B.

Select t. a, min (t. B) from t_tab_ AB group by t. a order by t.


3. Implementation result

A B

1 2

2 3

3 4


SQL groups the query results of a table according to a field, sorts the results by another field, and then sets an auto-increment field for each group of data.

Select * from table group by row1 order by row2
Set the auto-increment field again. This should be implemented through the trigger during data input.
 
I want to query all the data in a table in the database and sort it by group and order.

Select
Group column 1, group column 2 ...,
Aggregate column 1, aggregate Column 2...
From table
Group by group column 1, group column 2...
Order by group column or aggregate Column
--------
In a group query, the column displayed as the query result must be a group column or an aggregate column.
For example, sum (), count (), avg (), and so on.

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.