2012-12-24 116 views
0

我有這個巨大的表在Oracle中:如何在Oracle表中選擇一行?

COMPONENTSTATSID NUMBER(38,0) 
COMPONENTTYPEID NUMBER(38,0) 
NAME VARCHAR2(200 BYTE) 
SERIALNUMBER VARCHAR2(150 BYTE) 
WEIGHTKG NUMBER(9,0) 
SIZEWEIGHTMILIM NUMBER(6,0) 
SIZEWIDTHMILIM NUMBER(6,0) 
ZONECAGE VARCHAR2(50 BYTE) 
SIZELENGHTMILIM NUMBER(6,0) 
SIZEDEPTMILIM NUMBER(6,0) 
RAMSLOTS NUMBER 
NETWORKINTERFACE VARCHAR2(60 BYTE) 
RAMSLOTSPEED VARCHAR2(60 BYTE) 
RAMSLOTTYPE VARCHAR2(60 BYTE) 
POWERWATT NUMBER(5,0) 
OPERATINGTEMPERATUREMIN NUMBER(4,0) 
OPERATINGTEMPERATUREMAX NUMBER(4,0) 
STORAGETEMPERATUREMIN NUMBER(4,0) 
STORAGETEMPERATUREMAX NUMBER(4,0) 
DATEFORDECOMISN TIMESTAMP(6) 
MANIFACTURECOMPANY VARCHAR2(100 BYTE) 
MANIFACTUREWARRANTY VARCHAR2(100 BYTE) 
UFORM VARCHAR2(30 BYTE) 
STATUS VARCHAR2(30 BYTE) 
LOCATION VARCHAR2(30 BYTE) 
HEATEMISIONSBTU NUMBER(10,0) 
PRODUCTIONENVIRONMENT VARCHAR2(30 BYTE) 
STANDARTLIFETIME VARCHAR2(30 BYTE) 
OPERATINGHAMIDITYRANGE VARCHAR2(30 BYTE) 
BOUGTHDATE TIMESTAMP(6) 
OPERATINGSYSTEM CLOB 
SUPPORTCONTACT VARCHAR2(100 BYTE) 
INTERFACETYPE VARCHAR2(60 BYTE) 
TYPE VARCHAR2(30 BYTE) 
POWERCAPACITYWATT NUMBER(10,0) 
COOLINGCAPACITYBTU NUMBER(8,0) 
DATEADDED TIMESTAMP(6) 
STORAGECONTROLLER VARCHAR2(60 BYTE) 
DATEDEPLOYED TIMESTAMP(6) 
DESCRIPTION CLOB 
MAXNUMBERPROCESSORS NUMBER(2,0) 
CPUCACHESIZE VARCHAR2(30 BYTE) 
RAMSIZE VARCHAR2(30 BYTE) 
USBPORTS NUMBER(2,0) 
CPUCORES NUMBER 
RAIDSUPPORT VARCHAR2(30 BYTE) 
EXPANSIONSLOTS VARCHAR2(60 BYTE) 
DISKBAYS VARCHAR2(60 BYTE) 
HOTSWAPCOMPONENTS VARCHAR2(60 BYTE) 
NUMBERPORTS NUMBER(38,0) 
PORTSPEED VARCHAR2(30 BYTE) 
MINAMBIENTTEMP NUMBER(10,4) 
MAXAMBIENTTEMP NUMBER(10,4) 
DCALLOWEDWEIGHTKG NUMBER(10,4) 
DCMAXIMUMWEIGHTKG NUMBER(10,4) 
DCALLOWEDPOWERWATT NUMBER(10,4) 
DCMAXPOWERWATT NUMBER(10,4) 
DCALLOWCOOLINGPOWERBTU NUMBER(10,4) 
DCMAXCOOLINGPOWERBTU NUMBER(10,4) 
ISPBGPASNUMBER VARCHAR2(30 BYTE) 
BANDWIDTHUPLOADMB NUMBER(10,0) 
BANDWIDTHDOWNLOADMB NUMBER(10,0) 
IPVERSION VARCHAR2(30 BYTE) 
ISPIPRANGE VARCHAR2(30 BYTE) 

我用這個非常複雜的SQL查詢來選擇所有行到表:

SELECT a.* 
    FROM (SELECT b.*, rownum rn 
    FROM (select y.componentstatsid, y.name, y.status, y.location, y.type, y.powerCapacityWatt, y.coolingCapacityBTU, y.minAmbientTemp, y.maxAmbientTemp, 
    y.operatingHamidityRange, y.dcAllowedWeightKg, y.dcMaximumWeightKg, y.dcAllowedPowerWatt, y.dcMaxPowerWatt, y.dcAllowCoolingPowerBTU, y.dcMaxCoolingPowerBTU, y.datedeployed, y.dateadded, y.description 
    FROM component x, componentstats y where x.componentstatsid = y.componentstatsid and y.componenttypeid = 1000 
    ORDER BY %s %s) b 
    WHERE rownum <= ?) a  
    WHERE rn > ? 

的問題是我怎麼能簡化查詢選擇只有一行與componentstatsid = 35?沒有order by

+0

有超過一排用'componentStatsId = 35',或者是一個唯一的標識符(和可以簡單地添加到'WHERE'條款)?此外,應避免隱式連接語法(逗號分隔的'FROM'子句) - 請始終明確限定您的連接。 –

+0

如何在沒有'order by'的情況下產生一致的結果?你可以使用'where rownum = 1',但是不能保證每次都會得到相同的結果,而不必告訴DB你想要哪一行。 – Allan

回答

1

試試這個,

SELECT a.* 
     FROM (SELECT b.*, rownum rn 
     FROM (select y.componentstatsid, y.name, y.status, y.location, y.type, y.powerCapacityWatt, y.coolingCapacityBTU, y.minAmbientTemp, y.maxAmbientTemp, 
     y.operatingHamidityRange, y.dcAllowedWeightKg, y.dcMaximumWeightKg, y.dcAllowedPowerWatt, y.dcMaxPowerWatt, y.dcAllowCoolingPowerBTU, y.dcMaxCoolingPowerBTU, y.datedeployed, y.dateadded, y.description 
     FROM component x, componentstats y where x.componentstatsid = y.componentstatsid and y.componenttypeid = 1000 and y.componentstatsid=35 
     ORDER BY %s %s) b 
     WHERE rownum <= ?) a  
     WHERE rn > ?