如何将正值和负值分成一列中的两列。
我有一个表Employee
,其中Salary
列有正值和负值。我想提取两列NegSalary和PosSalary:
Salary ---> NegSalary PosSalary
-1000 -1000 NULL
2000 NULL 2000
1000 NULL 1000
500 NULL 500
答案 0 :(得分:5)
select case when col >= 0 then col else null end as pos,
case when col < 0 then col else null end as neg
from your_table
你可以在这里看到 - &gt; http://rextester.com/MCAW87762
答案 1 :(得分:1)
如果没有满足条件,则返回NULL。
select case when col >= 0 then col end as pos
,case when col < 0 then col end as neg
from mytable