你能在postgresql中做这样的事情吗?
select min(column1, column2) as Min, max(column1, column2) as Max from mytable;
答案 0 :(得分:3)
您想使用LEAST(a,b)sql函数。
在另一个堆栈溢出问题How do I get the MIN() of two fields in Postgres?
上回答了这个问题SELECT LEAST(column1, column2) as Min,
GREATEST(column1, column2) as Max
FROM mytable;
官方postgresql文档是here。
答案 1 :(得分:2)
选项1:
select CASE when a.min1 < a.min2 then a.min1 else a.min2 END as minimum,
CASE when a.max1 > a.max2 then a.max1 else a.max2 END as maximum
from (select min(col1) as min1, min(col2) as min2, max(col1) as max1,
max(col2) as max2 from myTable) a
选项2:
select CASE when MIN(col1) < MIN(col2) THEN MIN(col1) ELSE MIN(col2) END as Minimum,
CASE WHEN MAX(col1) > MAX(col2) THEN MAX(col1) ELSE MAX(col2) END as Maximum
from myTable
最终选项:
select LEAST(MIN(col1),MIN(col2)) as myMinimum, GREATEST(MAX(col1),MAX(col2)) as myMaximum from myTable
这个怎么样? :)
答案 2 :(得分:0)
select
min(CASE when a.min1 < a.min2 then a.min1 else a.min2 END) as minimum,
max(CASE when a.max1 > a.max2 then a.max1 else a.max2 END) as maximum
from myTable as a