【问题标题】:replacing the column with next column values [closed]用下一列值替换该列[关闭]
【发布时间】:2013-02-28 12:42:04
【问题描述】:
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查询从数据库中反映出来的

输出:

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

我需要以下方式的输出。 假设如果任何一列是空白的,那么它应该被下一列值替换。

【问题讨论】:

  • 请检查第一个输出中的 adrln3 值,它是空白的,在下一个输出中它有下一列值
  • 您可能想要编辑示例数据以仅显示 相关 列。

标签: oracle plsql oracle10g oracle11g


【解决方案1】:

您应该使用合并。例如,

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;

【讨论】:

  • @Florin Ghita 感谢你的绝招
猜你喜欢
  • 2023-03-15
  • 1970-01-01
  • 2013-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-18
  • 1970-01-01
  • 2019-01-04
相关资源
最近更新 更多