获取列的次要值的常用函数是min(column),但我想做的是根据3列的值获取一行的次要值。例如,使用以下基表:
+------+------+------+
| col1 | col2 | col3 |
+------+------+------+
| 2 | 1 | 3 |
| 10 | 0 | 1 |
| 13 | 12 | 2 |
+------+------+------+我想按如下方式查询:
+-----------+
| min_value |
+-----------+
| 1 |
| 0 |
| 2 |
+-----------+我找到了一个解决方案,如下所示,但是针对SQL,而不是Postgresql。所以我不能让它在postgresql中工作:
select
(
select min(minCol)
from (values (t.col1), (t.col2), (t.col3)) as minCol(minCol)
) as minCol
from t我可以使用PostgreSQL语句编写一些东西,但我想为case编写一个类似上面的查询。这个是可能的吗?
发布于 2020-11-11 21:56:21
您可以使用least() (最大值为greatest() )
select least(col1, col2, col3) as min_value
from the_tablehttps://stackoverflow.com/questions/64787796
复制相似问题