I have a table in our ERP system which lists the days in a year that are marked in the system as Working Days (0) and Non-Working Days (1). There is 1 calendar row per year per charging code (BH/NBH).
For example, if I run this query:
SELECT CODE,[YEAR],DAYS#1, DAYS#2, DAYS#3, DAYS#4, DAYS#5, DAYS#6, DAYS#7
FROM Calendars
WHERE [YEAR] = 2019
I get:
+------+------+--------+--------+--------+--------+--------+--------+--------+
| CODE | YEAR | DAYS#1 | DAYS#2 | DAYS#3 | DAYS#4 | DAYS#5 | DAYS#6 | DAYS#7 |
+------+------+--------+--------+--------+--------+--------+--------+--------+
| BH | 2019 | 1 | 0 | 0 | 0 | 1 | 1 | 0 |
| NBH | 2019 | 0 | 0 | 0 | 0 | 1 | 1 | 0 |
+------+------+--------+--------+--------+--------+--------+--------+--------+
But I need to transform this so that there is a line for each day for each charging code and indicate the working / non-working days like this:
+------+------------+---------+
| CODE | DATE | NONWORK |
+------+------------+---------+
| BH | 2019-01-01 | 1 |
| BH | 2019-01-02 | 0 |
| BH | 2019-01-03 | 0 |
| BH | 2019-01-04 | 0 |
| BH | 2019-01-05 | 1 |
| BH | 2019-01-06 | 1 |
| BH | 2019-01-07 | 0 |
+------+------------+---------+
How can I do this so that it processes all calendars in existence for all years and charging codes? Here is a table creation script to assist:
CREATE TABLE [dbo].[Calendars](
[CODE] [varchar](4) NULL,
[YEAR] [varchar](4) NULL,
[DAYS#1] [numeric](2, 0) NULL,
[DAYS#2] [numeric](2, 0) NULL,
[DAYS#3] [numeric](2, 0) NULL,
[DAYS#4] [numeric](2, 0) NULL,
[DAYS#5] [numeric](2, 0) NULL,
[DAYS#6] [numeric](2, 0) NULL,
[DAYS#7] [numeric](2, 0) NULL
)
and test data:
INSERT INTO CALENDARS (CODE, YEAR, DAYS#1, DAYS#2, DAYS#3, DAYS#4, DAYS#5, DAYS#6, DAYS#7) VALUES ('BH',2019,1,0,0,0,1,1,0)
INSERT INTO CALENDARS2 (CODE, YEAR, DAYS#1, DAYS#2, DAYS#3, DAYS#4, DAYS#5, DAYS#6, DAYS#7) VALUES ('NBH',2019,0,0,0,0,1,1,0)
Any assistance would be appreciated.