如何将一列数据分成两列

时间:2022-08-04 13:07:01

how to separate positive and negative values into two columns from one column.

如何将正值和负值从一列分成两列。

I have a table Employee with a column Salary that has positive and negative value. I want to extract two column NegSalary and PosSalary like this:

我有一个表Employee,其列薪水具有正值和负值。我想提取两列NegSalary和PosSalary,如下所示:

Salary              --->   NegSalary       PosSalary
-1000                       -1000           NULL
 2000                        NULL           2000
 1000                        NULL           1000
 500                         NULL           500

2 个解决方案

#1


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

You can see this here -> http://rextester.com/MCAW87762

你可以在这里看到 - > http://rextester.com/MCAW87762

#2


1  

Case returns NULL if no condition was met.

如果没有满足条件,则Case返回NULL。

select  case when col >= 0 then col end  as pos
       ,case when col <  0 then col end  as neg

from    mytable

#1


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

You can see this here -> http://rextester.com/MCAW87762

你可以在这里看到 - > http://rextester.com/MCAW87762

#2


1  

Case returns NULL if no condition was met.

如果没有满足条件,则Case返回NULL。

select  case when col >= 0 then col end  as pos
       ,case when col <  0 then col end  as neg

from    mytable