日期格式:20200409,20190922求月份差
hive: 经过测试: select months_between('2020-04','2019-09')from mid.dual--nullselect months_between('2020-04-02','2019-09-02')from mid.dual--7select months_between('2020-04-02','2019-09-20')from mid.dual--6.41935484select months_between('2020-04-20','2019-09-02')from mid.dual--7.58064516select unix_timestamp(substr(20200408,1,6),'yyyyMM')from mid.dual--1585670400select from_unixtime(unix_timestamp(substr(20200409,1,6),'yyyyMM'),'yyyy-MM-dd')from mid.dual--2020-04-01最终: select months_between(from_unixtime(unix_timestamp(substr(20200409,1,6),'yyyyMM'),'yyyy-MM-dd'), from_unixtime(unix_timestamp(substr(20190922,1,6),'yyyyMM'),'yyyy-MM-dd')) from mid.dual--7
oracle: select to_date('202004','YYYYMM')from dual--2020/4/1select months_between(to_date('20200402','YYYYMMdd'),to_date('20190920','YYYYMMdd'))from dual--6.41935483870968select months_between(to_date('20200420','YYYYMMdd'),to_date('20190902','YYYYMMdd'))from dual--7.58064516129032select months_between(to_date('202004','YYYYMM'),to_date('201909','YYYYMM'))from dual--7
SELECT TO_date('202004','yyyymm')FROM mid.DUAL--2020/4/1SELECT add_months(TO_date('202004','yyyymm'),-1)FROM mid.DUAL--2020/3/1SELECT TO_CHAR(add_months(TO_date('202004','yyyymm'),-1),'yyyymm')FROM mid.DUAL--202003select unix_timestamp('202004','yyyyMM')from mid.dual--1585670400select from_unixtime(unix_timestamp('202004','yyyyMM'),'yyyy-MM-dd')from mid.dual--2020-04-01select add_months(from_unixtime(unix_timestamp('202004','yyyyMM'),'yyyy-MM-dd'),-1)from mid.dual--2020-03-01select unix_timestamp(add_months(from_unixtime(unix_timestamp(sbustr('202004',1,6),'yyyyMM'),'yyyy-MM-dd'),-1),'yyyy-MM')from mid.dual--1582992000select from_unixtime(unix_timestamp(add_months(from_unixtime(unix_timestamp('202004','yyyyMM'),'yyyy-MM-dd'),-1),'yyyy-MM'),'yyyyMM')from mid.dual--202003