首页 > 解决方案 > 案例陈述同一列

问题描述

有一列,其中包含总计和小时的值,但我需要将组分成两列,例如

select 
case
when A.TYPE = 'H'

then A.Value


end as "Hours",

Case 
when A.TYPE != 'H'
then A.VALUE

end as "Total" 

from a

这返回的是 2 列,但将其加倍不是衬里值。

Hours     Total
  2        null 
null        20

标签: sql

解决方案


您的表格似乎有两行。如果您想要结果集中的一行,则需要聚合:

select max(case when A.TYPE = 'H' then A.Value end) as Hours,
       max(case when A.TYPE <> 'H' then A.VALUE end) as Total
from a;

推荐阅读