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
Release: 2025-01-06 13:10:38
Original
193 people have browsed it

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
)
Copy after login

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
Copy after login

Executing the query would yield the following results:

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

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template