Since you're including speed
in your select and group, and those values vary per row, your current query will basically return the full table just with the MAX(technology)
for each row. This is because speed
can't be grouped into a single value as they are all different.
ie.
ID technology speed
1 5G 20
1 5G 10
1 5G 40
1 5G 100
Based purely on your sample set, you could select the MAX(speed)
since it always coincides with the MAX(technology)
, and then you would get the right results:
ID technology speed
1 5G 100
However, if the MAX(technology)
ever has less than the MAX(speed)
, the above would become incorrect.
A better approach would be to use a window function because you would remove that potential flaw:
with cte as (
SELECT ID, technology, speed,
ROW_NUMBER() OVER (PARTITION BY ID ORDER BY technology DESC) RN
FROM table)
SELECT *
FROM cte
WHERE RN = 1
This assigns a number to each row, starting with number 1 for the row that has the MAX(technology)
(ie. ORDER BY technology DESC
), and does this for each ID
(ie. PARTITION BY ID
).
Therefore when we select only the rows that are assigned row number 1, we are getting the full row for each max technology / id combination.
One last note - if there are duplicate rows with the same ID
and technology
but with various speeds, this would pick one of them at random. You would need to further include an ORDER
for speed
in that case. Based on your sample set this doesn't happen, but just an fyi.