SQL efficient screening: Get the minimum value of each unique identifierIn database management, it is often necessary to extract records with unique values and its corresponding minimum values. Suppose there is a form containing the unique identifier column and associated values. The goal is to retrieve the rows corresponding to the minimum value from each unique identifier.
Challenge
Consider the following table:
The goal is to find the minimum
line corresponding to each uniquevalue. The expected output is as follows:
id game point 1 x 5 1 z 4 2 y 6 3 x 2 3 y 5 3 z 8
game
Solutionpoint
You can use the following SQL query to achieve this:
id game point 1 z 4 2 y 6 3 x 2 Explanation
Internal query calculates the minimum<code class="language-sql">SELECT tbl.* FROM TableName tbl INNER JOIN ( SELECT Id, MIN(Point) AS MinPoint FROM TableName GROUP BY Id ) tbl1 ON tbl1.id = tbl.id WHERE tbl1.MinPoint = tbl.Point;</code>Copy after loginvalue of each unique and stores the result in the derivative table .
The main inquiry will connect the original table
- with
In the end, it filter the result, only contains the minimumId
according to the publicPoint
column.tbl1
- value (from
Id
) and the line ofTableName
in the original table.tbl1
- By using this query, you can efficiently retrieve the unique row of
Point
in thetbl1
group.Point
The above is the detailed content of How to Retrieve Rows with Minimum Values for Each Unique Identifier in SQL?. For more information, please follow other related articles on the PHP Chinese website!