By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
Help with an interesting Postgres question: Why isn't an Index Only Scan used on a partition accessed via the parent table?.
create table mytable as
select 1 as foo, 2 as bar, -1 as fuz union all
select 3 as foo, 10, -7 union all
select 2 as foo, 5, -2 union all
select 8 as foo, -2, 10 union all
select 10 as foo, 0, 10
5 rows affected
select foo, bar,
(case when foo - bar < -5 then -5
when foo - bar > 5 then 5
else foo - bar
end)
from mytable
foo | bar | case |
---|---|---|
1 | 2 | -1 |
3 | 10 | -5 |
2 | 5 | -3 |
8 | -2 | 5 |
10 | 0 | 5 |
select foo, bar,
least(greatest(foo - bar, -5), 5)
from mytable
foo | bar | least |
---|---|---|
1 | 2 | -1 |
3 | 10 | -5 |
2 | 5 | -3 |
8 | -2 | 5 |
10 | 0 | 5 |