Home >Database >Mysql Tutorial >How to Find the Maximum Signal Value for Each Unique ID in a Dataset?

How to Find the Maximum Signal Value for Each Unique ID in a Dataset?

DDD
DDDOriginal
2025-01-06 13:10:38195browse

How to Find the Maximum Signal Value for Each Unique ID in a Dataset?

Retrieving Maximum Signal Values for Unique IDs

In order to determine the maximum signal value for each unique ID in a given dataset, you can employ the following approach, which involves a self-join operation:

SELECT cur.id, cur.signal, cur.station, cur.ownerid
FROM yourtable cur
WHERE NOT EXISTS (
    SELECT *
    FROM yourtable high
    WHERE high.id = cur.id AND high.signal > cur.signal
)

This query works by joining the table with itself (represented by the aliases cur and high) and filtering out rows for which a higher signal value exists. The result is a list of rows that contain the ID, signal value, station, and ownerID for each distinct ID with the highest signal value.

In the context of the example table provided:

ID     Signal    Station    OwnerID
111     -120      Home       1
111     -130      Car        1
111     -135      Work       2
222     -98       Home       2
222     -95       Work       1
222     -103      Work       2

Executing the query would yield the following results:

ID    Signal    Station    OwnerID
111   -120      Home        1
222   -95       Work        1

This table contains the maximum signal value for each unique ID, effectively solving the original problem without unnecessary data aggregation or the need for additional table joins.

The above is the detailed content of How to Find the Maximum Signal Value for Each Unique ID in a Dataset?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn