查看以下查询:
SELECT *
FROM ENI_FLUSSI_HUB c1
WHERE flh_tipo_processo_cod IN ('VA', 'NUOVA_ATT_ENI')
AND rownum < 10
它只是提取一些VA。我需要提取一些VA和一些NUOVA_ATT_ENI。
这是最优雅的方式吗?
答案 0 :(得分: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所述,您始终可以使用view
进行此选择。
答案 1 :(得分:1)
这是一个很好的过度设计的解决方案:
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
/
我认为它所做的是为flh_tipe_processo_cod的两个值中的每一个返回至少一行,并且“让自然与其他人一起”。
您必须编辑主查询中的*,以避免尝试包含前两个子查询因子子句中的rowid。
这是另一个,我认为试图将每个中的五个带回来但是如果两个子查询因子条款中的任何一个可用少于五个,那么将“补充”所需的总数:
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
/
享受!