【问题标题】:DB2 Select from not empty columns with column nameDB2 从具有列名的非空列中选择
【发布时间】:2019-07-12 21:59:31
【问题描述】:

我在 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

【问题讨论】:

    标签: sql db2


    【解决方案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
    

    【讨论】:

      【解决方案2】:

      用例陈述。

      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
      

      【讨论】:

        猜你喜欢
        • 2011-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多