我的原始查询:
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数据库上运行这个查询的。
2条答案
按热度按时间tquggr8v1#
不要使用
union
,而是将这些查询中的每一个都用作CTE(稍做修改-包括稍后将在JOIN中使用的行号):样本数据:
查询从此处开始:
t_start
和t_end
表示当前查询最后:
qgzx9mmu2#
您还可以使用MODEL子句来避免两次扫描表: