I'm trying to write a Snowflake SQL statement that does the following:
For example, if my table is:
column_A | column_B |
---|---|
-20 | 1 |
-5 | 2 |
1 | 3 |
15 | 4 |
The result should be: -5, 2
If my table is:
column_A | column_B |
---|---|
1 | 3 |
15 | 4 |
20 | 5 |
The result should be: 1, 3
To create an example table:
with example_table as (
select
$1::NUMBER as column_A
, $2::NUMBER as column_B
from
(values
(-20, 1)
, (-5, 2)
, (1, 3)
, (15, 4)
)
)
select * from example_table
Something like:
order by
case when column_a < 0 then 1 else 2 end,
abs(column_a)
offset 0 rows
fetch first 1 row only
Basically you order by
on two faux columns: