oracle 如何将不同行的查询结果合并为一个结果?

wkyowqbh  于 2023-02-11  发布在  Oracle
关注(0)|答案(2)|浏览(261)

我的原始查询:

SELECT desc, start_date
FROM foo.bar
WHERE desc LIKE 'Fall%' AND desc NOT LIKE '%Med%'
UNION
SELECT desc, end_date
FROM foo.bar
WHERE desc LIKE 'Spring%' AND desc NOT LIKE '%Med%'
ORDER BY start_date;

通过这个查询,我得到了(大概)我想要的数据集。现在我需要获取这些数据,并按顺序合并每次获取两个数据的结果,然后生成如下结果:
| 描述|开始日期|结束日期|
| - ------|- ------|- ------|
| 1971年秋季-1972年 Spring |一九七一年八月十五日|一九七二年五月十五日|
| 1971年秋季-1972年 Spring |一九七二年八月十五日|一九七三年五月十五日|
其中DESC是第1行和第2行中DESC的串联,START_DATE是第1行中的日期,END_DATE是第2行中的日期。整个数据集都遵循相同的模式。
任何帮助查询,将产生我需要的结果是非常感谢。不知道我是否走在正确的道路或如果原来的查询只是错误的。
如上所述,我尝试了提供的查询,它提供了我需要的数据。但是,我没有找到一种方法将它格式化为我想要的输出。还应该注意,我是在Oracle数据库上运行这个查询的。

tquggr8v

tquggr8v1#

不要使用union,而是将这些查询中的每一个都用作CTE(稍做修改-包括稍后将在JOIN中使用的行号):
样本数据:

SQL> with test (description, datum) as
  2    (select 'Fall 1971'  , date '1971-08-15' from dual union all
  3     select 'Spring 1972', date '1972-05-15' from dual union all
  4     select 'Fall 1972'  , date '1972-08-15' from dual union all
  5     select 'Spring 1973', date '1973-05-15' from dual union all
  6     select 'Fall 1973'  , date '1973-08-15' from dual union all
  7     select 'Spring 1974', date '1974-05-15' from dual union all
  8     select 'Fall 1974'  , date '1974-08-15' from dual union all
  9     select 'Spring 1975', date '1975-05-15' from dual
 10    ),

查询从此处开始:t_startt_end表示当前查询

11  t_start as
 12    (select description, datum,
 13       row_number() Over (order by datum) rn
 14     from test
 15     where description like 'Fall%' and description not like '%Med%'
 16    ),
 17  t_end as
 18    (select description, datum,
 19       row_number() Over (order by datum) rn
 20     from test
 21     where description like 'Spring%' and description not like '%Med%'
 22    )

最后:

23  select s.description ||' - '|| e.description as description,
 24    s.datum start_date,
 25    e.datum end_date
 26  from t_start s join t_end e on s.rn = e.rn
 27  order by s.rn;

DESCRIPTION               START_DAT END_DATE
------------------------- --------- ---------
Fall 1971 - Spring 1972   15-AUG-71 15-MAY-72
Fall 1972 - Spring 1973   15-AUG-72 15-MAY-73
Fall 1973 - Spring 1974   15-AUG-73 15-MAY-74
Fall 1974 - Spring 1975   15-AUG-74 15-MAY-75

SQL>
qgzx9mmu

qgzx9mmu2#

您还可以使用MODEL子句来避免两次扫描表:

with data(description,datum) as (
    select 'Fall 1971'  , date '1971-08-15' from dual union all
    select 'Spring 1972', date '1972-05-15' from dual union all
    select 'Fall 1972'  , date '1972-08-15' from dual union all
    select 'Spring 1973', date '1973-05-15' from dual union all
    select 'Fall 1973'  , date '1973-08-15' from dual union all
    select 'Spring 1974', date '1974-05-15' from dual union all
    select 'Fall 1974'  , date '1974-08-15' from dual union all
    select 'Spring 1975', date '1975-05-15' from dual
)
select description, start_date, end_date
from (
    select rn, desc1 as description, start_date, end_date
    from (
        select row_number() over(order by datum) as rn, description, datum
        from data
        where description not like '%Med%'
    )
    model 
        dimension by (rn)
        measures (
            cast(' ' as varchar2(256)) as desc1, description, cast(NULL as DATE) start_date, cast(NULL as DATE) end_date , datum
        )
        rules (
            desc1[mod(rn,2)=1] = description[cv()] || ' - ' || description[cv()+1],
            start_date[mod(rn,2)=1] = datum[cv()], 
            end_date[mod(rn,2)=1] = datum[cv()+1] 
        )
)
where mod(rn,2)=1
;

相关问题