OK, Starting with your query:
select*
from(select PRESSURE, ID2, TABLE.PRESSUREAVG,
rank()over(partition by ID2 order by ABS(PRESSURE-0.85))rank
from TABLE.PRESSUREAVG)
where rank<=1;
The first problem I see is formatting. Maybe the forum did that. But you should put some daylight in there so things look nice and are easier to read. So, adding a space before *, and indention in the subquery, and space around the OVER keyword and before the RANK alias at the end of the 2nd line. Now we’ve got
select *
from(select PRESSURE, ID2, TABLE.PRESSUREAVG,
rank() over (partition by ID2 order by ABS(PRESSURE-0.85)) rank
from TABLE.PRESSUREAVG)
where rank<=1;
The second problem I see is the table name. Normally, If I see a dot inside a table name, then the stuff before the dot is the schema and the stuff after the dot is the table name. And “TABLE” is a reserved word, so it shouldn’t be either one. But if the whole thing really is the table name, then it needs to be enclosed in double-quotes, because a dot really shouldn’t be part of a table name. So, if the table is really called TABLE.PRESSUREAVG, then you need to always refer to it as “TABLE.PRESSUREAVG” (case is important once you start using double quotes)
So now we have…
select *
from(select PRESSURE, ID2, “TABLE.PRESSUREAVG”,
rank() over (partition by ID2 order by ABS(PRESSURE-0.85)) rank
from “TABLE.PRESSUREAVG”)
where rank<=1;
The next problem…what is table name doing in the select list of the subquery? Table names should only appear in FROM clauses (except when used as aliases, but that’s not what’s happening here). Fixing that, we get…
select *
from(select PRESSURE, ID2,
rank() over (partition by ID2 order by ABS(PRESSURE-0.85)) rank
from “TABLE.PRESSUREAVG”)
where rank<=1;
and now the query runs, but it doesn’t exactly show your desired result…it shows
PRESSURE
ID2
RANK
1.5
1A
1
1
1B
1
So, to fix that, we can rearrange the from that * to what we actually want (and add ID1 to the subquery’s select list)
select ID1, ID2, PRESSURE
from(select PRESSURE, ID1, ID2,
rank() over (partition by ID2 order by ABS(PRESSURE-0.85)) rank
from “TABLE.PRESSUREAVG”)
where rank<=1;
and that gives you what you want…
ID1
ID2
PRESSURE
1A-3
1A
1.5
1B-2
1B
1