2017-09-16 83 views
0

我需要从返回一行的不同行中选择数据。 我的表是这样的:如何在Oracle中获取不同行的值为单行

SNO USEFUL COUNTER PINCODE SUBSNO DATA       REFERENCE 
--- ------ ------- ------- ------ ----------------------------- --------- 
1 Y  null 504293 null  504293        Sl.No 
2 null null 504293 null  1         null 
3 null null 504293 null  iciseva00031      null 
4 null null 504293 null  SANTHOSH KUMAR      null 
5 null null 504293 null  SANTHOSH MANTHENA     null 
6 null null 504293 null  12-52 BRAHMAN WADA    null 
7 null null 504293 null  ASIFABAD       null 
8 null null 504293 null  Andhra Pradesh      null 
9 null null 504293 null  9248859222       null 
10 null null 504293 null  2         null 
11 null null 504293 null  WSGLDPL12415      null 
12 null null 504293 null  SIDDHARTHA COMMUNICATIONS   null 
13 null null 504293 null  MASADE SATISH      null 
14 null null 504293 null  HNO10-143POSTOFFICEROADBAPUNAGAR null 
            ASIFABADDISTKUMRAMBHEEMASIFABAD 
15 null null 504293 null  ADILABAD       null 
16 null null 504293 null  ANDHRA PRADESH      null 
17 null null 504293 null  9059187009       null 

我不得不这样返回的输出:

PINCODE SUBSNO USER   COMPANY  AGENT   ADDRESS CITY   STATE  PHONE 
------- ------ ----   -------  -----   ------- ----   -----  ----- 
504293 1  iciseva00031 SANTHOSHKUMAR SANTHOSHMANTHENA 12-52 BRAHMAN WADA ASIFABAD Andhra Pradesh 9248859222 

请帮助我。

+0

源表中缺少数据,例如手机。 – mjpolak

回答

0

“我必须表明产出表是这样的:”

这是很容易抢一行(有正则表达式的支持,以帮助缠斗重复使用广告编辑):

select t1.data as PINCODE 
     , t2.data as SUBSNO 
     , t3.data as USER 
     , t4.data as COMPANY 
     , t5.data as AGENT 
     , t6.data as ADDRESS 
     , t7.data as CITY 
     , t8.data as STATE 
     , t9.data as PHONE 
from (select PINCODE, DATA from your_table where PINCODE = DATA) t1        
    left join (select PINCODE, DATA from your_table where SNO = 2) t2 
     on t2.pincode = t1.pincode        
    left join (select PINCODE, DATA from your_table where SNO = 3) t3 
     on t3.pincode = t1.pincode        
    left join (select PINCODE, DATA from your_table where SNO = 4) t4 
     on t4.pincode = t1.pincode        
    left join (select PINCODE, DATA from your_table where SNO = 5) t5 
     on t5.pincode = t1.pincode        
    left join (select PINCODE, DATA from your_table where SNO = 6) t6 
     on t6.pincode = t1.pincode        
    left join (select PINCODE, DATA from your_table where SNO = 7) t7 
     on t7.pincode = t1.pincode        
    left join (select PINCODE, DATA from your_table where SNO = 8) t8 
     on t8.pincode = t1.pincode        
    left join (select PINCODE, DATA from your_table where SNO = 9) t9 
     on t9.pincode = t1.pincode   
where t1.pincode = '504293' 
/

当然,这可能不是你所需要的。毫无疑问,你希望拥有所有额外的行 - sno >= 10。简单:只要不断重复上述过程就可以获得所需数量的列。

备注

  1. 您是否必须对投影中的列名进行硬编码?是的。所呈现的值表没有属性类型指示符,因此无法知道该列表示什么。所以你需要自己将缺少的智能注入查询。
  2. 如果结果表中确实有一个属性类型列,您可以使用它来标记投影中的列吗?是的,但它意味着动态生成查询。
  3. 如果另一个PINCODE以不同的顺序包含列,会发生什么情况?你会怎么说?如果没有属性类型指示符,您不知道任何列应该是什么,所有您必须继续是由您的示例输出表示的推测优先级。
  4. 所有这些加入:不会表现吸?是的,是的,它可能会。

不幸的是你没有简单的。优雅的,表现性的解决方案,因为源数据是一场车祸。

这些通用数据存储表对于开发人员编写数据输入例程具有诱惑力。他们可以以一种非常简单的方式存储数据,而不必担心数据建模和所有这些繁琐的工作。另外它是超级灵活的和面向未来的。但是他们实际上做了什么,却蒙受了大量的技术债务,必须由需要获取数据的开发者来偿还。

相关问题