2

次のクエリを見てください。

SELECT *
FROM ENI_FLUSSI_HUB c1
WHERE flh_tipo_processo_cod IN ('VA', 'NUOVA_ATT_ENI')
AND rownum < 10

VA の一部だけを抽出するだけです。いくつかの VA といくつかの NUOVA_ATT_ENI を抽出する必要があります。

それを行う最もエレガントな方法は何ですか?

4

2 に答える 2

2

あなたはこのようにそれを行うことができます:

SELECT *
FROM ENI_FLUSSI_HUB c1
WHERE flh_tipo_processo_cod = 'VA'
AND rownum < 5

UNION 

SELECT *
FROM ENI_FLUSSI_HUB c1
WHERE flh_tipo_processo_cod = 'NUOVA_ATT_ENI'
AND rownum < 5

重複する値はありませんか?を使用UNION ALLして、より高速に実行できます。

SELECT *
FROM ENI_FLUSSI_HUB c1
WHERE flh_tipo_processo_cod = 'VA'
AND rownum < 5

UNION ALL

SELECT *
FROM ENI_FLUSSI_HUB c1
WHERE flh_tipo_processo_cod = 'NUOVA_ATT_ENI'
AND rownum < 5

@DavidAldridgeが述べたように、いつでもaを使用viewしてこの選択を行うことができます。

于 2012-05-18T09:45:42.617 に答える
1

Here's a nicely overengineered solution:

with
  va as (
    select rowid ri,
           t.*
    from   eni_flussi_hub t
    where  flh_tipo_processo_cod = 'VA'
    and    rownum <= 1),
  nuova_att_eni as (
    select rowid ri,
           t
    from   eni_flussi_hub t
    where  flh_tipo_processo_cod = 'NUOVA_ATT_ENI'
    and    rownum <=1),
  the_rest as (
    select *
    from   eni_flussi_hub c1
    where  flh_tipo_processo_cod in ('VA','NUOVA_ATT_ENI')
    and    rowid not in (
             select ri
             from   va
             union all
             select ri
             from nuova_att_eni)
    and rownum <=9)
select *
from  (
      select * from va
      union all
      select * from nuova_att_eni
      union all
      select * from the_rest
      )
where rownum <= 10
/

I think that what it does is return at least one row for each of the two values of flh_tipe_processo_cod, and "lets nature take its course" with the rest.

You'd have to edit the *'s in the main query to avoid trying to include the rowid from the first two subquery factoring clauses.

Here's another, which I think attempts to bring back five of each but will "top up" the required total if less than five are available for either of the two subquery factoring clauses:

with
  va as (
    select rownum rn,
           t.*
    from   eni_flussi_hub t
    where  flh_tipo_processo_cod = 'VA'
    and    rownum <= 10),
  nuova_att_eni as (
    select rownum rn,
           t
    from   eni_flussi_hub t
    where  flh_tipo_processo_cod = 'NUOVA_ATT_ENI'
    and    rownum <=10)
select *
from  (
      select *
      from   (select * from va
              union all
              select * from nuova_att_eni)
      order by rn asc
      )
where rownum <= 10
/

Enjoy!

于 2012-05-18T10:05:27.180 に答える