DB2中有一个包含以下数据的表:
Location|Phone|Email |Changedatetime
null |3314 |null |12/07/2019 10:00
null |null |e@e.com|12/07/2019 11:00
这是相关表中的更改列表。 我需要基于此表的选择,列名称和值不能为空
如果可能,我需要通过以下方式选择它:
Attribute|Value |Changedatetime
Phone |3314 |12/07/2019 10:00
Email |e@e.com|12/07/2019 11:00
答案 0 :(得分:1)
一种方法可能是使用union
select 'Phone' as attribute, Phone as value, Changedatetime
from tablename where phone is not null
union
select 'Email', email, Changedatetime
from tablename where email is not null
答案 1 :(得分:1)
用例声明。
select
case when Phone is not null then 'Phone' else 'Email' end as Attribute ,
case when Phone is not null then Phone else Email end as Value ,
Changedatetime
from tablename