Sql 输入年份和月份,如('20182020','1,2,4,5')和带有每个月名称的返回年份
输入年份和月份,如'20182020'、'1,2,4,5'和带有每个月名称的返回年份Sql 输入年份和月份,如('20182020','1,2,4,5')和带有每个月名称的返回年份,sql,oracle,plsql,Sql,Oracle,Plsql,输入年份和月份,如'20182020'、'1,2,4,5'和带有每个月名称的返回年份 i.e - 2018-jan 2020-jan 2018-feb 2020-feb .. .. 下面是如何慢慢地、小心地完成的 SQL> with test (col) as 2 (select q'[('2018,2020','1,2,4,5')]' from dual), 3 t_years as 4 (selec
i.e - 2018-jan
2020-jan
2018-feb
2020-feb
..
..
下面是如何慢慢地、小心地完成的
SQL> with test (col) as
2 (select q'[('2018,2020','1,2,4,5')]' from dual),
3 t_years as
4 (select substr(col, instr(col, chr(39), 1, 1) + 1,
5 instr(col, chr(39), 1, 2) - instr(col, chr(39), 1, 1) - 1) yrs
6 from test
7 ),
8 t_months as
9 (select substr(col, instr(col, chr(39), 1, 3) + 1,
10 instr(col, chr(39), 1, 4) - instr(col, chr(39), 1, 3) - 1) mon
11 from test
12 ),
13 row_years as
14 (select regexp_substr(ty.yrs, '[^,]+', 1, level) yrs
15 from t_years ty
16 connect by level <= regexp_count(ty.yrs, ',') + 1
17 ),
18 row_months as
19 (select regexp_substr(tm.mon, '[^,]+', 1, level) mon
20 from t_months tm
21 connect by level <= regexp_count(tm.mon, ',') + 1
22 )
23 select
24 to_char(to_date(ry.yrs ||' '|| rm.mon, 'yyyy mm'), 'yyyy mon') result
25 from row_years ry cross join row_months rm;
RESULT
-----------------
2018 jan
2018 feb
2018 apr
2018 may
2020 jan
2020 feb
2020 apr
2020 may
8 rows selected.
SQL>
t_年和t_月分别从输入字符串中提取20182020和1,2,4,5
行年和行月将列拆分为行
最终结果表示行\年和行\月之间的交叉联接,其值被连接起来,并具有到\字符、到\日期的适当函数以及应用于它们的适当格式掩码
非常感谢您,先生,不客气。