如何在Oracle中每年添加缺少的月份 [英] How to add missing months in each year in oracle

查看:208
本文介绍了如何在Oracle中每年添加缺少的月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我查询的输出

但是我需要在2016年和2017年的所有月份中使用

but I need all months in 2016 and 2017 in the output means

01-2017,02-2017,03-2017,04-2017,05-2017,06-2017,07-2017,08-2017,09-2017,10-2017,11-2017,12-2017 ,01-2016,01-2016,01-2016,02-2016,03-2016,04-2016,05-2016,06-2016,07-2016,08-2016,09-2016,10-2016,11 -2016,12-2016]

01-2017,02-2017,03-2017,04-2017,05-2017, 06-2017, 07-2017, 08-2017,09-2017,10-2017,11-2017,12-2017,01-2016,01-2016,01-2016,02-2016,03-2016,04-2016,05-2016,06-2016,07-2016,08-2016,09-2016,10-2016,11-2016,12-2016]

我的查询是:

SELECT DISTINCT
    to_char(to_date(checked_date, 'dd-mon-yy'), 'mm-yyyy') AS month
FROM DOCTOR_CHECKUP;

下图显示了我的查询的输出,但是我需要2016年和2017年的所有月份,其输出方式为01-2017、02-2017、03-2017、04-2017、05-2017、06-2017, 07-2017、08-2017、09-2017、10-2017、11-2017、12-2017、01-2016、01-2016、01-2016、02-2016、03-2016、04-2016、05- 2016,06-2016,07-2016,08-2016,09-2016,10-2016,11-2016,12-2016

The below image shows the output of my query, but I need all months in 2016 and 2017 in the output means 01-2017,02-2017,03-2017,04-2017,05-2017, 06-2017, 07-2017, 08-2017,09-2017,10-2017,11-2017,12-2017,01-2016,01-2016,01-2016,02-2016,03-2016,04-2016,05-2016,06-2016,07-2016,08-2016,09-2016,10-2016,11-2016,12-2016

推荐答案

这很简单.显然,当您只想从'01-2016''12-2017'选择月份时,您甚至不需要该表:

This is simple. Obviously you don't even need the table when you simply want to select months from '01-2016' to '12-2017':

select '01-2016' as month from dual
union all
select '02-2016' as month from dual
union all
...
union all
select '12-2017' as month from dual

这篇关于如何在Oracle中每年添加缺少的月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆