I understand that this type of procedure is NOT preferable nor efficient, but I am at a point where this type of table is needed since I have not been able to get a response to my original question (Fill In for Null Values for Date, Product Code & QTY) on here. I found this relevant post/answer (While loop with multiple conditions in T-SQL) but need it adapted to my scenario. As I am only a data-querying guy I haven't a clue what I am doing in this while begin loop that I failed to BM where I drafted it from.
I am in the same scenario as the user in the aforementioned post, I don't want to have to create this type of table but am out of options and help. Am very much hoping I can get some help/answers as the user above did.
Help me ObiOneOverflow, you're my only hope!
I am trying to create a daily reference record for each product code between two dates, looping on the ROWID till it gets to the last ROWID integer. It is breaking somewhere, I just don't have the experience in this type of programming to figure out why nor where, I HAVE tried changing parts to see how it reacts, but never getting a successful run.
I hope you pros can simply see where I'm causing it to fail on a loop and keep iterating up between the dates, then restarting on the next ROWID from the product table.
CREATE TABLE FACT_ECODEMONTHLY
(
STARTDT DATE NOT NULL,
MONTHEND DATE NOT NULL,
ECODE VARCHAR (255)
);
DECLARE @STARTDT DATE
DECLARE @ENDDT DATE
DECLARE @TEMPDT DATE
DECLARE @START INT
DECLARE @END INT
DECLARE @TEMPNO INT
DECLARE @ROWID INT
DECLARE @ECODE VARCHAR (255)
SET @STARTDT = '2017-01-01'
SET @ENDDT = '2022-12-31'
SELECT @START = MIN(DE1.ROW_ID), @END = MAX(DE1.ROW_ID) FROM DIM_ECODES DE1
SET @ECODE = ( SELECT DE2.ECODE FROM DIM_ECODES DE2 WHERE ROW_ID = @START )
BEGIN
WHILE (@STARTDT <= @ENDDT) AND (@START <= @END )
BEGIN
SET @TEMPDT = @STARTDT;
SET @TEMPDT = DATEADD( DAY , 1 , @TEMPDT)
WHILE (@START <= @END )
BEGIN
SET @TEMPNO = @START;
SET @TEMPNO = @TEMPNO + 1
INSERT INTO FACT_ECODEMONTHLY
( STARTDT , MONTHEND , ECODE )
VALUES
( @STARTDT , EOMONTH(@STARTDT) , @ECODE )
IF (@STARTDT <= @ENDDT )
BEGIN
SET @STARTDT = @TEMPDT
END
ELSE
BEGIN
SET @STARTDT = '2017-01-01'
SET @ENDDT = DATEADD (DAY , 1 , @STARTDT)
END
IF (@START <= @END)
BEGIN
SET @START = @TEMPNO
END
ELSE
BEGIN
SET @START = 1
SET @END = @START + 1
END
END
END
END
When I run the above, I get the following:
Thank you for looking and any assistance provided.
Schema (MySQL v8.0)
CREATE TABLE DIM_ECODES
(
ECODE VARCHAR(255) NOT NULL,
MCODE VARCHAR(255) NOT NULL,
SOURCE VARCHAR(255) NOT NULL,
ROW_ID INT
);
INSERT INTO DIM_ECODES
(ECODE , MCODE , SOURCE , ROW_ID )
VALUES
('Q5142-80','Q5999-571','TEAM',1),
('P1DH6-','P1DH6-', 'TEAM',2),
('U616Z-051','U520U-','TEAM',3),
('14404-31','14404-31', 'TEAM',4),
('R6980-','R6980-', 'TEAM',5);
Query #1
SELECT *
FROM DIM_ECODES;
| ECODE | MCODE | SOURCE | ROW_ID |
|---|---|---|---|
| Q5142-80 | Q5999-571 | TEAM | 1 |
| P1DH6- | P1DH6- | TEAM | 2 |
| U616Z-051 | U520U- | TEAM | 3 |
| 14404-31 | 14404-31 | TEAM | 4 |
| R6980- | R6980- | TEAM | 5 |
DESIRED RESULTS EXAMPLE
| DATE | EOM | ECODE |
|---|---|---|
| 1/1/2017 | 1/31/2017 | Q5142-80 |
| 1/2/2017 | 1/31/2017 | Q5142-80 |
| 1/3/2017 | 1/31/2017 | Q5142-80 |
| 1/4/2017 | 1/31/2017 | Q5142-80 |
| 1/5/2017 | 1/31/2017 | Q5142-80 |
| 1/6/2017 | 1/31/2017 | Q5142-80 |
| 1/7/2017 | 1/31/2017 | Q5142-80 |
| 1/8/2017 | 1/31/2017 | Q5142-80 |
| 1/9/2017 | 1/31/2017 | Q5142-80 |
| 1/10/2017 | 1/31/2017 | Q5142-80 |
| 1/11/2017 | 1/31/2017 | Q5142-80 |
| 1/12/2017 | 1/31/2017 | Q5142-80 |
| 1/13/2017 | 1/31/2017 | Q5142-80 |
| 1/14/2017 | 1/31/2017 | Q5142-80 |
| 1/15/2017 | 1/31/2017 | Q5142-80 |
| 1/16/2017 | 1/31/2017 | Q5142-80 |
| 1/17/2017 | 1/31/2017 | Q5142-80 |
| 1/18/2017 | 1/31/2017 | Q5142-80 |
| 1/19/2017 | 1/31/2017 | Q5142-80 |
| 1/20/2017 | 1/31/2017 | Q5142-80 |
| 1/21/2017 | 1/31/2017 | Q5142-80 |
| 1/22/2017 | 1/31/2017 | Q5142-80 |
| 1/23/2017 | 1/31/2017 | Q5142-80 |
| 1/24/2017 | 1/31/2017 | Q5142-80 |
| 1/25/2017 | 1/31/2017 | Q5142-80 |
| 1/26/2017 | 1/31/2017 | Q5142-80 |
| 1/27/2017 | 1/31/2017 | Q5142-80 |
| 1/28/2017 | 1/31/2017 | Q5142-80 |
| 1/29/2017 | 1/31/2017 | Q5142-80 |
| 1/30/2017 | 1/31/2017 | Q5142-80 |
| 1/31/2017 | 1/31/2017 | Q5142-80 |
| 1/1/2017 | 1/31/2017 | P1DH6- |
| 1/2/2017 | 1/31/2017 | P1DH6- |
| 1/3/2017 | 1/31/2017 | P1DH6- |
| 1/4/2017 | 1/31/2017 | P1DH6- |
| 1/5/2017 | 1/31/2017 | P1DH6- |
| 1/6/2017 | 1/31/2017 | P1DH6- |
| 1/7/2017 | 1/31/2017 | P1DH6- |
| 1/8/2017 | 1/31/2017 | P1DH6- |
| 1/9/2017 | 1/31/2017 | P1DH6- |
| 1/10/2017 | 1/31/2017 | P1DH6- |
| 1/11/2017 | 1/31/2017 | P1DH6- |
| 1/12/2017 | 1/31/2017 | P1DH6- |
| 1/13/2017 | 1/31/2017 | P1DH6- |
| 1/14/2017 | 1/31/2017 | P1DH6- |
| 1/15/2017 | 1/31/2017 | P1DH6- |
| 1/16/2017 | 1/31/2017 | P1DH6- |
| 1/17/2017 | 1/31/2017 | P1DH6- |
| 1/18/2017 | 1/31/2017 | P1DH6- |
| 1/19/2017 | 1/31/2017 | P1DH6- |
| 1/20/2017 | 1/31/2017 | P1DH6- |
| 1/21/2017 | 1/31/2017 | P1DH6- |
| 1/22/2017 | 1/31/2017 | P1DH6- |
| 1/23/2017 | 1/31/2017 | P1DH6- |
| 1/24/2017 | 1/31/2017 | P1DH6- |
| 1/25/2017 | 1/31/2017 | P1DH6- |
| 1/26/2017 | 1/31/2017 | P1DH6- |
| 1/27/2017 | 1/31/2017 | P1DH6- |
| 1/28/2017 | 1/31/2017 | P1DH6- |
| 1/29/2017 | 1/31/2017 | P1DH6- |
| 1/30/2017 | 1/31/2017 | P1DH6- |
| 1/31/2017 | 1/31/2017 | P1DH6- |