使用pivot在oracle 11g中进行数据格式化

时间:2016-03-17 12:08:36

标签: oracle pivot self-join

我在oracle 11g中有一个表ATTRIBUTES

order           attribute                            value
-------------------------------------------------------------------------
802605          CL_ACC844FIBRE_SERVICE                  N

802605          CL_ACC844FIBRE_SERVICE_LOCATION         N

802605          CL_ACC844FLOOR                          GROUND

802605           CL_ACC844HAZARD                        BTSS  

802605          CL_ACC844HOUSING                        CABINET

802605          CL_ACC844TEAM                           EDINBURGH

802605          CL_ACC844LANDLORDS_CONSENT                Y

802605         CL_ACC844LANDLORDS_NECESSARY                N

802605         CL_ACC844LANDLORD_NAME                   NOT APPLICABLE

802605          CL_ACC844LANDLORD_PHONE                 NOT APPLICABLE

802605          CL_ACC844LOCATION                       ROOM 

802605         CL_ACC844MAINTENANCE_CAT                 FIBRE 

802605         CL_ACC844NTE_CHASSIS                     SINGLE SLOT

802605         CL_ACC844OCCUPANCY_COMMENCE_DATE         17/02/2012

每个订单包含数百个这样的属性

我想要数据的格式是这样的

ORDER   TECH  BLEACH  AF1        AF2            AF3         AF4     EF
-----------------------------------------------------------------------------
802605   N     YES  11/10/2007  11/10/2007  11/10/2007  11/10/2007  1/10/2007

我用来获取数据的查询是

SELECT a1.order ,
       a1.value tech  ,
       a2.value bleach  ,
       a3.value af1  ,
       a4.value af2  ,
       a5.value af3  ,
       a6.value af4  ,
       a7.value ef  
  FROM attributes a1,
       attributes a2,
       attributes a3,
       attributes a4,
       attributes a5,
       attributes a6,
       attributes a7
 WHERE a1.order = '802605-S844'
         AND a1.order = a2.order
         AND ( a1.atribute LIKE 'CL_ACC%G_ADSL_DETAILS_ACCESS_TECHNOLOGY'
         OR a2.atribute LIKE 'CL_ACC%G_VCG_DETAILS1COS_BLEACHING_FLAG'
         OR a3.atribute LIKE 'CL_ACC%G_VCG_DETAILS1G_6LAYER_COS_CIPR_AF1'
         OR a4.atribute LIKE 'CL_ACC%G_VCG_DETAILS1G_6LAYER_COS_CIPR_AF2'
         OR a5.atribute LIKE 'CL_ACC%G_VCG_DETAILS1G_6LAYER_COS_CIPR_AF3'
         OR a6.atribute LIKE 'CL_ACC%G_VCG_DETAILS1G_6LAYER_COS_CIPR_AF4'
         Or A7.atribute Like 'CL_ACC%G_VCG_DETAILS1G_6LAYER_COS_CIPR_EF' );

我知道这不是使用自联接格式化数据的正确方法,而且我必须获取400个订单的数据,因此查询需要数天来显示数据。 有人可以提供更好的格式化数据的方法。我尝试过使用PIVOT但是无法得到它。

1 个答案:

答案 0 :(得分:1)

import os, sys, time

def textfile():
   f = open("old.txt", "w")
   x = input("name for your file: ")
   os.rename("old.txt", "%x.txt)
   f.write("This is a sentence")
   f.close()

textfile()