用下一列值替换列

时间:2013-02-28 12:42:05

标签: oracle plsql oracle10g oracle11g

ADRLN1                                   ADRLN2                                   ADRLN3                                   ADRLN4                                   ADRCIT                    ADRSTA                    ADRCNY                    ADDRESS
---------------------------------------- ---------------------------------------- ---------------------------------------- ---------------------------------------- ------------------------- ------------------------- ------------------------- ----------
Tsrdl                                    address1                                 address2                                  dfdfdfdfdfdfdfdfdfdfdfdfdf              Alibaug                   Maharashtra               India                     412001
aaa aa Ltd.                              Mahalaxmi                                                                                                                  Mumbai                    Maharashtra                                         400011
190, SANDESH VIHAR (P&T)                 DELHI                                                                                                                                                                                                    110034
6/2/A LLOYDS GARDEN                      APPASAHEB MARATHE MARG                   PRABHADEVI                               MUMBAI                                                                                                                 400025

以上数据使用select query

从数据库中反映出来

输出:

ADRLN1                                   ADRLN2                                   ADRLN3                                   ADRLN4                                   ADRCIT                    ADRSTA                    ADRCNY                    ADDRESS
---------------------------------------- ---------------------------------------- ---------------------------------------- ---------------------------------------- ------------------------- ------------------------- ------------------------- ----------
Tsrdl                                    address1                                 address2                                  dfdfdfdfdfdfdfdfdfdfdfdfdf              Alibaug                   Maharashtra               India                     412001
aaa aa Ltd.                              Mahalaxmi                                Mumbai                                    Maharashtra                             400011
190, SANDESH VIHAR (P&T)                 DELHI                                    110034
6/2/A LLOYDS GARDEN                      APPASAHEB MARATHE MARG                   PRABHADEVI                               MUMBAI                                   400025

我需要以下列方式输出。 假设任何列为空,则应替换为下一列值。

1 个答案:

答案 0 :(得分:1)

你应该使用coalesce。例如,

select coalesce (ADRLN1, ADRLN2, ADRLN3, ADRLN4 ) as ADRLN1 
  coalesce (ADRLN2, ADRLN3, ADRLN4 ) as ADRLN2,
  coalesce (ADRLN3, ADRLN4 ) as ADRLN3,
  ADRLN2 as ADRLN2
from table;

但你应该继续使用逻辑来真正改变你的专栏。

修改:似乎您想要消除地址中的“空白”。

您可以做的一个技巧是选择列的串联:

select decode(ADRLN3, null, null, ADRLN3||',')||
   decode(ADRLN4, null, null, ADRLN4||',')||
   decode(ADRCIT, null, null, ADRCIT||',')||
   decode(ADRSTA, null, null, ADRSTA||',')||
   decode(ADRCNY, null, null, ADRCNY||',')||
   ADDRESS  as ADDRESS
from table;