0

我正在尝试重构具有五个字段的 Select 连接的查询,因此

SELECT
   Id
   '-'
   Field_2
   '-'
   Field_3
   '-'
   Field_4
    AS id
 FROM some_table

示例结果将是这样的

Id

1-a-b-c-d

2-e-f-g-h

#null#
#null#

我的问题是,虽然此查询有效,但这些字段中的部分或全部可能为空。有人建议,当所有或任何这些字段为空时,交叉连接可以允许显示空。不幸的是,我不知道该怎么做。由于行数较多,建议使用交叉连接以提高性能。

有人可以告诉我如何

4

3 回答 3

1

您没有提供很多细节,但我希望您想要的是以下内容。

SELECT COALESCE(Id,'') || '-' || 
       COALESCE(Field_2,'') || '-' ||
       COALESCE(Field_3,'') || '-' ||
       COALESCE(Field_4,'')
    AS id
 FROM some_table
于 2019-08-01T18:09:50.013 回答
0

试试这个
select nvl(concat(concat(concat(concat(a,b),c),d),e),'NULL') from some_table

于 2019-08-01T18:30:07.743 回答
0

当我们将任何字段显示为 null 并带有单词 null

这里可以加入什么?使用简单case

with sample_data(id, field1, field2, field3, field4) as (
    select 1,  'a',  'b',  'c',  'd' from dual union all
    select 2,  'e',  'f',  'g',  'h' from dual union all
    select 3,  'p',  'q', null,  's' from dual union all
    select 4, null,  'x',  'y',  'z' from dual )

select case when id is null or field1 is null or field2 is null or field3 is null or field4 is null
            then '#null#'
            else id || '-' || field1 || '-' || field2 || '-' || field3 || '-' || field4
       end as id
  from sample_data

结果:

ID
---------
1-a-b-c-d
2-e-f-g-h
#null#
#null#
于 2019-08-02T12:44:22.203 回答