简体   繁体   English

MySQL 根据当前日期动态分配月份

[英]MySQL Dynamic assignment of month based on current date

Hope you are doing well..I am trying to convert a plan table as below希望你过得好..我正在尝试如下转换计划表

Input                                                   
Segment | Model |FC1 |FC2 |FC3 |FC4 |FC5 |  FC6 | FC7 | FC8 | FC9 | FC10 |  FC11 | FC12
HRX P3412   9   14  11  22  17  23  18  15  23  12  12  19
SRX O321    11  8   8   9   9   16  19  7   22  12  11  15
SRX LD12    14  10  20  22  18  19  10  17  21  16  10  21
HRX M421    17  18  16  12  14  17  10  16  8   8   7   23
MRX N342    3   23  16  13  20  9   16  14  16  17  10  11
HRX J231    4   10  20  20  21  23  17  22  14  15  8   22

into the table below based on the current date and the reference table根据当前日期和参考表进入下表

Segment |Model| Apr-22 |May-22 |Jun-22 |Jul-22 |Aug-22 |Sep-22|Oct-22|Nov-22    | Dec-22 |  Jan-23 |Feb-23 |Mar-23
HRX P3412   9   14  11  22  17  23  18  15  23  12  12  19
SRX O321    11  8   8   9   9   16  19  7   22  12  11  15
SRX LD12    14  10  20  22  18  19  10  17  21  16  10  21
HRX M421    17  18  16  12  14  17  10  16  8   8   7   23
MRX N342    3   23  16  13  20  9   16  14  16  17  10  11
HRX J231    4   10  20  20  21  23  17  22  14  15  8   22

Reference table:参考表:

Fiscal Month    From    to
Jan-22  Dec 26 2021     Jan 22 2022
Feb-22  Jan 23 2022 19-Feb-22
Mar-22  20-Feb-22   26-Mar-22
Apr-22  27-Mar-22   23-Apr-22
May-22  24-Apr-22   21-May-22
Jun-22  22-May-22   25-Jun-22
Jul-22  26-Jun-22   23-Jul-22
Aug-22  24-Jul-22   20-Aug-22
Sep-22  21-Aug-22   24-Sep-22
Oct-22  25-Sep-22   22-Oct-22
Nov-22  23-Oct-22   19-Nov-22
Dec-22  20-Nov-22   31-Dec-22

So I need to basically map the column names (FC1,FC2,FC3...input table) to fiscal month based on the current date and looking up the reference table for the fiscal month... Can you please help me here..所以我需要基本上 map 列名称(FC1,FC2,FC3 ...输入表)到基于当前日期的财政月份并查找财政月份的参考表...你能帮我吗..

The column names should change every fiscal month according to the reference table dynamically..For example FC1 should be renamed to May 2022 and FC2 should be renamed to June 2022 from 24th April 2022...Similarly from 22nd may 2022 FC1 should be renamed to June 2022, FC2 should be renamed to July 2022...列名应根据参考表在每个财政月动态更改。例如,FC1 应重命名为 2022 年 5 月,FC2 应从 2022 年 4 月 24 日起重命名为 2022 年 6 月...同样,从 2022 年 5 月 22 日起,FC1 应重命名为2022年6月,FC2应该更名为2022年7月...

Please find the DDL for the tables:请找到表的 DDL:

create table input
(segment varchar(40),
model varchar (40),
FC1 int,
FC2 int,
FC3 int,
FC4 int,
FC5 int,
FC6 int,
FC7 int,
FC8 int,
FC9 int,
FC10 int,
FC11 int,
FC12 int)

insert into input values
('HRX','P3412','9','14','11','22','17','23','18','15','23','12','12','19'),
('SRX','O321','11','8','8','9','9','16','19','7','22','12','11','15'),
('SRX','LD12','14','10','20','22','18','19','10','17','21','16','10','21'),
('HRX','M421','17','18','16','12','14','17','10','16','8','8','7','23'),
('MRX','N342','3','23','16','13','20','9','16','14','16','17','10','11'),
('HRX','J231','4','10','20','20','21','23','17','22','14','15','8','22')

create table output
(segment varchar(40),
model varchar(40),
Apr2022 int,
May2022 int,
Jun2022 int,
jul2022 int,
aug2022 int,
sep2022 int,
oct2022 int,
nov2022 int,
dec2022 int,
Jan2023 int,
feb2023 int,
mar2023 int)

insert into output values
('HRX','P3412','9','14','11','22','17','23','18','15','23','12','12','19'),
('SRX','O321','11','8','8','9','9','16','19','7','22','12','11','15'),
('SRX','LD12','14','10','20','22','18','19','10','17','21','16','10','21'),
('HRX','M421','17','18','16','12','14','17','10','16','8','8','7','23'),
('MRX','N342','3','23','16','13','20','9','16','14','16','17','10','11'),
('HRX','J231','4','10','20','20','21','23','17','22','14','15','8','22')

create table reference
(fiscalmonth varchar(40),
from date,
to date
)

insert into reference values
('Jan 2022','Dec 26 2021 ','Jan 22 2022'),
('Feb 2022','Jan 23 2022','Feb 19 2022'),
('March 2022','feb 20 2022','Mar 26 2022'),
('April 2022','Mar 27 2022','Apr 23 2022'),
('May 2022','Apr 24 2022','May 21 2022'),
('June 2022','May 22 2022','Jun 25 2022'),
('July 2022','June 26 2022','Jul 23 2022'),
('Aug 2022','Jul 24 2022','Aug 20 2022'),
('Sep 2022','Aug 21 2022','Sep 24 2022'),
('Oct 2022','Sep 25 2022','Oct 22 2022'),
('Nov 2022','Oct 23 2022','Nov 19 2022'),
('Dec 2022','Nov 20 2022','Dec 31 2022')

We can fetch the first 12 rows starting with the current month, and number them using row_number.我们可以获取从当前月份开始的前 12 行,并使用 row_number 对它们进行编号。 We then do a manual pivot, using rn so that it will not need changing from month to month.然后我们使用 rn 做一个手册 pivot,这样它就不需要每个月都改变了。
The only thing that will need to be kept up to date is the references table.唯一需要保持最新的是参考表。 (I have added some months for 2023 using the same dates as 2022. (我使用与 2022 年相同的日期为 2023 年添加了几个月。

 select '' segment, '' model, max(case when rn = 1 then fiscalmonth end) FC1, max(case when rn = 2 then fiscalmonth end) FC2, max(case when rn = 3 then fiscalmonth end) FC3, max(case when rn = 4 then fiscalmonth end) FC4, max(case when rn = 5 then fiscalmonth end) FC5, max(case when rn = 6 then fiscalmonth end) FC6, max(case when rn = 7 then fiscalmonth end) FC7, max(case when rn = 8 then fiscalmonth end) FC8, max(case when rn = 9 then fiscalmonth end) FC9, max(case when rn = 10 then fiscalmonth end) FC10, max(case when rn = 11 then fiscalmonth end) FC11, max(case when rn = 12 then fiscalmonth end) FC12 from (select row_number() over (order by to_ ) as rn, fiscalmonth from reference where to_ >= curdate() limit 12) as months union all select * from input
 segment |段 | model | model | FC1 | FC1 | FC2 | FC2 | FC3 | FC3 | FC4 | FC4 | FC5 | FC5 | FC6 | FC6 | FC7 | FC7 | FC8 | FC8 | FC9 | FC9 | FC10 | FC10 | FC11 | FC11 | FC12:------ |:---- |:--------- |:------- |:-------- |:-------- |:------- |:------- |:------- |:------- |:------- |:------- |:------- |:--------- | FC12:------ |:---- |:-------- |:-------- |:-------- |:---- ---- |:-------- |:-------- |:-------- |:-------- |:------ | :-------- |:-------- |:-------- | | | April 2022 | 2022 年 4 月 | May 2022 | 2022 年 5 月 | June 2022 | 2022 年 6 月 | July 2022 | 2022 年 7 月 | Aug 2022 | 2022 年 8 月 | Sep 2022 | 2022 年 9 月 | Oct 2022 | 2022 年 10 月 | Nov 2022 | 2022 年 11 月 | Dec 2022 | 2022 年 12 月 | Jan 2023 | 2023 年 1 月 | Feb 2023 | 2023 年 2 月 | March 2023 HRX | 2023 年 3 月 HRX | P3412 | P3412 | 9 | 9 | 14 | 14 | 11 | 11 | 22 | 22 | 17 | 17 | 23 | 23 | 18 | 18 | 15 | 15 | 23 | 23 | 12 | 12 | 12 | 12 | 19 SRX | 19 SRX | O321 | O321 | 11 | 11 | 8 | 8 | 8 | 8 | 9 | 9 | 9 | 9 | 16 | 16 | 19 | 19 | 7 | 7 | 22 | 22 | 12 | 12 | 11 | 11 | 15 SRX | 15 SRX | LD12 | LD12 | 14 | 14 | 10 | 10 | 20 | 20 | 22 | 22 | 18 | 18 | 19 | 19 | 10 | 10 | 17 | 17 | 21 | 21 | 16 | 16 | 10 | 10 | 21 HRX | 21HRX | M421 | M421 | 17 | 17 | 18 | 18 | 16 | 16 | 12 | 12 | 14 | 14 | 17 | 17 | 10 | 10 | 16 | 16 | 8 | 8 | 8 | 8 | 7 | 7 | 23 MRX | 23 MRX | N342 | N342 | 3 | 3 | 23 | 23 | 16 | 16 | 13 | 13 | 20 | 20 | 9 | 9 | 16 | 16 | 14 | 14 | 16 | 16 | 17 | 17 | 10 | 10 | 11 HRX | 11HRX | J231 | J231 | 4 | 4 | 10 | 10 | 20 | 20 | 20 | 20 | 21 | 21 | 23 | 23 | 17 | 17 | 22 | 22 | 14 | 14 | 15 | 15 | 8 | 8 | 22 22

db<>fiddle here db<> 在这里摆弄

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM