0

最初の select ステートメントから返された値に基づいて、一連の select ステートメントを実行したいと考えています。基本的に元の値をループし、その値を新しい選択の基準として使用します。

私がやろうとしていることのいくつかの擬似コード(シェルでこれを書く方法...):

for location in `select places from tablename where XYZ`
do
   select new_field from tablename where location = '$location';
done

これが私が本当に実行したい選択です。

ラックのリストを入手してください:

select   regexp_substr("MYTABLE"."Serial_Number" ,'[^ ]+', 1, 3) as "Racks"
  from   "MYTABLE" "MYTABLE" 
 where   "MYTABLE"."Data_Center" ='SOMEPLACE' 
   and   "MYTABLE"."Device_Type" ='RACK'
   and   "MYTABLE"."Serial_Number" not like '%WAREHOUSE%'

デバイスの数に基づいてラックを所有する必要があるユーザーを出力します。

select count(*) as count, LOB 
 from    "MYTABLE" "MYTABLE" 
 where   "MYTABLE"."Data_Center" ='SOMEPLACE' 
   and   GRID_LOCATION = '$RACK_from_above' and rownum <= 1 group by LOB order by count desc;

前もって感謝します!

4

2 に答える 2

2

テーブルをそれ自体に結合するだけです(2つの異なるエイリアスを使用)

select count(1) as count, MYTABLE1.LOB, MYTABLE1.GRID_LOCATION  
from    "MYTABLE" "MYTABLE1" , "MYTABLE" "MYTABLE2"   
where   MYTABLE1.Data_Center ='SOMEPLACE'    
 and   MYTABLE1.GRID_LOCATION = regexp_substr(MYTABLE2.Serial_Number ,'[^ ]+', 1, 3)
 and   MYTABLE1.rownum <= 1
 and   MYTABLE2.Data_Center ='SOMEPLACE'
 and   MYTABLE2.Device_Type ='RACK'
 and   MYTABLE2.Serial_Number not like '%WAREHOUSE%'
group by MYTABLE1.LOB, MYTABLE1.GRID_LOCATION
order by count desc;

(私には思われる)明白な理由のために、group by 句と select 句にグリッドの場所を追加したことに注意してください。それが不明な場合は、別の質問として尋ねることができます。

于 2012-08-14T23:08:50.443 に答える
0

これらを 1 つのクエリに簡単に組み合わせることができます。

with thelist as (
    select   regexp_substr("MYTABLE"."Serial_Number" ,'[^ ]+', 1, 3) as "Racks"
      from   "MYTABLE" "MYTABLE" 
     where   "MYTABLE"."Data_Center" ='SOMEPLACE' 
       and   "MYTABLE"."Device_Type" ='RACK'
       and   "MYTABLE"."Serial_Number" not like '%WAREHOUSE%'
)
select count(*) as count, LOB 
from    "MYTABLE" "MYTABLE" 
where   "MYTABLE"."Data_Center" ='SOMEPLACE' and
      GRID_LOCATION in (select racks from thelist) and
      rownum <= 1
group by LOB
order by count desc;
于 2012-08-14T23:10:02.603 に答える