Calculated field
LEAST([amount], [other])
Sample data
| id | amount | units | score | other |
|---|---|---|---|---|
| 1 | -2.5 | 2 | -0.8 | 3 |
| 2 | 0 | 0 | 0 | 4 |
| 3 | 1.5 | 3 | 0.25 | 2 |
| 4 | 4 | 4 | 0.5 | 8 |
| 5 | 9 | 5 | 0.8 | 11 |
| 6 | 16 | 6 | 1 | 14 |
| 7 | null | 7 | null | 9 |
| 8 | 4 | 8 | -0.5 | null |
The same calculation as SQL
SELECT least(amount,other) AS result FROM input ORDER BY id
Runs the example above.
SELECT LEAST_IGNORE_NULLS(amount,other) AS result FROM input ORDER BY id
Written for this dialect; not run here.
SELECT (SELECT min(v) FROM unnest([amount,other]) AS v) AS result FROM input ORDER BY id
Written for this dialect; not run here.
Result
| id | amount | units | score | result |
|---|---|---|---|---|
| 1 | -2.5 | 2 | -0.8 | -2.5 |
| 2 | 0 | 0 | 0 | 0 |
| 3 | 1.5 | 3 | 0.25 | 1.5 |
| 4 | 4 | 4 | 0.5 | 4 |
| 5 | 9 | 5 | 0.8 | 9 |
| 6 | 16 | 6 | 1 | 14 |
| 7 | null | 7 | null | 9 |
| 8 | 4 | 8 | -0.5 | 4 |
Same rows from the calculation and from the SQL in DuckDB.
Where a naive translation goes wrong
- Null-propagating extrema discard a present argument.
Naive SQL
SELECT CASE WHEN amount IS NULL OR other IS NULL THEN NULL ELSE least(amount,other) END AS result FROM input ORDER BY id
id amount units score result 1 -2.5 2 -0.8 -2.5 2 0 0 0 0 3 1.5 3 0.25 1.5 4 4 4 0.5 4 5 9 5 0.8 9 6 16 6 1 14 7 null 7 null null 8 4 8 -0.5 null Matches the calculation
SELECT least(amount,other) AS result FROM input ORDER BY id
id amount units score result 1 -2.5 2 -0.8 -2.5 2 0 0 0 0 3 1.5 3 0.25 1.5 4 4 4 0.5 4 5 9 5 0.8 9 6 16 6 1 14 7 null 7 null 9 8 4 8 -0.5 4
Signature
LEAST(number, number, ...)