I'm looking for a script to create an new table based on an other.
Input table:
OutputTable:
Original was built in QlikView but now I have to convert it to SQL
I'm looking for a script to create an new table based on an other.
Input table:
OutputTable:
Original was built in QlikView but now I have to convert it to SQL
Your question is not crystal clear, but I'll take a shot at what i think you want. This is a great scenario for using a calendar table! I've abbreviated the calendar table here, and used US style date, but you get the idea. Also, in your example, I think you've added an extraneous row in the results. There probably should not be a row with 1-4-2020 X B 2.
DECLARE @cal TABLE
(
dt DATE
);
INSERT @cal
(
dt
)
VALUES
('04-01-2020'),('04-02-2020'),('04-03-2020'),('04-04-2020'),
('04-05-2020'),('04-06-2020'),('04-07-2020'),('04-08-2020'),
('04-09-2020'),('04-10-2020'),('04-11-2020'),('04-12-2020'),
('04-13-2020'),('04-14-2020'),('04-16-2020'),('04-17-2020'),
('04-18-2020'),('04-19-2020'),('04-20-2020'),('04-21-2020'),
('04-22-2020'),('04-23-2020'),('04-24-2020'),('04-25-2020'),
('04-26-2020'),('04-27-2020'),('04-28-2020'),('04-29-2020'),
('04-30-2020');
DECLARE @Input TABLE
(
ProjectID CHAR(1),
Item CHAR(1),
Quantity INT,
Startdate DATE,
EndDate DATE
);
INSERT @Input
(
ProjectID,
Item,
Quantity,
Startdate,
EndDate
)
VALUES
( 'X',
'A',
1,
'04-01-2020',
'04-05-2020'),
( 'Y',
'A',
2,
'04-01-2020',
'04-06-2020'),
( 'X',
'B',
2,
'04-02-2020',
'04-05-2020');
/* Join to the calendar table. */
SELECT c.dt,
i.ProjectID,
i.Item,
i.Quantity
FROM @Input AS i
JOIN @cal AS c
ON c.dt
BETWEEN i.startdate AND i.EndDate
ORDER BY i.ProjectID, i.Item, c.dt