简体   繁体   中英

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..

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...

Please find the DDL for the tables:

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. We then do a manual pivot, using rn so that it will not need changing from month to month.
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.

 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 |  FC1 |  FC2 |  FC3 |  FC4 |  FC5 |  FC6 |  FC7 |  FC8 |  FC9 |  FC10 |  FC11 |  FC12:------ |:---- |:--------- |:------- |:-------- |:-------- |:------- |:------- |:------- |:------- |:------- |:------- |:------- |:--------- |  |  April 2022 |  May 2022 |  June 2022 |  July 2022 |  Aug 2022 |  Sep 2022 |  Oct 2022 |  Nov 2022 |  Dec 2022 |  Jan 2023 |  Feb 2023 |  March 2023 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 

db<>fiddle here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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