I have a table of timesheet data from our ERP system and I need to split it into 7 rows, one for each day of the week and add the date for that day based on the column name and weekending date.
Here is some partial data from the source database:
+----------+----------------------------------+------------+----------+-------------------+----------+----------+----------+----------+----------+----------+----------+-----------+-----------+
| RECORDER | CIRECID | ENDDATE | ITEMNO | MEMO | STDHRS#1 | STDHRS#2 | STDHRS#3 | STDHRS#4 | STDHRS#5 | STDHRS#6 | STDHRS#7 | OTHRS#1#1 | OTHRS#2#1 |
+----------+----------------------------------+------------+----------+-------------------+----------+----------+----------+----------+----------+----------+----------+-----------+-----------+
| 91 | 0A186D8CDB6B499C8BFEBF104C023C6C | 08/08/2021 | 14MC1136 | Adrian Baxanean | 9 | 9 | 9 | 9 | 9 | 0 | 0 | 2 | 1 |
| 93 | 911BD813528546089A3102CDB5F5BE26 | 08/08/2021 | 14MC0513 | Scott Fleming | 9 | 9 | 9 | 9 | 9 | 0 | 0 | 1 | 1 |
| 100 | 0A186D8CDB6B499C8BFEBF104C023C6C | 15/08/2021 | 14MC1136 | Adrian Baxanean | 9 | 9 | 9 | 9 | 9 | 0 | 0 | 2 | 2 |
+----------+----------------------------------+------------+----------+-------------------+----------+----------+----------+----------+----------+----------+----------+-----------+-----------+
This should be split into 7 rows for each row, with three columns added for Standard Hours and Overtime Hours 1 & 2 (STDHRS, OTHRS1 and OTHRS2). The date should then be calculated based on the ENDDATE which is always a Sunday, the hours columns
STDHRS#1 is Monday
STDHRS#2 is Tuesday etc.
OTHRS#1#1 is Monday
OTHRS#2#1 is Tuesday etc.
(The second overtime rate is not shown in the data above but would be the same format with OTHRS#1#2 being Monday, OTHRS#2#2 being Tuesday etc.)
For the source data above, this is what needs to be returned:
+----------+----------------------------------+------------+----------+-------------------+------------+--------+---------+---------+
| RECORDER | CIRECID | ENDDATE | ITEMNO | MEMO | DATE | STDHRS | OTHRS#1 | OTHRS#2 |
+----------+----------------------------------+------------+----------+-------------------+------------+--------+---------+---------+
| 91 | 0A186D8CDB6B499C8BFEBF104C023C6C | 08/08/2021 | 14MC1136 | Adrian Baxanean | 02/08/2021 | 9 | 2 | 0 |
| 91 | 0A186D8CDB6B499C8BFEBF104C023C6C | 08/08/2021 | 14MC1136 | Adrian Baxanean | 03/08/2021 | 9 | 1 | 0 |
| 91 | 0A186D8CDB6B499C8BFEBF104C023C6C | 08/08/2021 | 14MC1136 | Adrian Baxanean | 04/08/2021 | 9 | 0 | 0 |
| 91 | 0A186D8CDB6B499C8BFEBF104C023C6C | 08/08/2021 | 14MC1136 | Adrian Baxanean | 05/08/2021 | 9 | 0 | 0 |
| 91 | 0A186D8CDB6B499C8BFEBF104C023C6C | 08/08/2021 | 14MC1136 | Adrian Baxanean | 06/08/2021 | 9 | 0 | 0 |
| 91 | 0A186D8CDB6B499C8BFEBF104C023C6C | 08/08/2021 | 14MC1136 | Adrian Baxanean | 07/08/2021 | 0 | 0 | 0 |
| 91 | 0A186D8CDB6B499C8BFEBF104C023C6C | 08/08/2021 | 14MC1136 | Adrian Baxanean | 08/08/2021 | 0 | 0 | 0 |
| 93 | 911BD813528546089A3102CDB5F5BE26 | 08/08/2021 | 14MC0513 | Scott Fleming | 02/08/2021 | 9 | 1 | 0 |
| 93 | 911BD813528546089A3102CDB5F5BE26 | 08/08/2021 | 14MC0513 | Scott Fleming | 03/08/2021 | 9 | 1 | 0 |
| 93 | 911BD813528546089A3102CDB5F5BE26 | 08/08/2021 | 14MC0513 | Scott Fleming | 04/08/2021 | 9 | 0 | 0 |
| 93 | 911BD813528546089A3102CDB5F5BE26 | 08/08/2021 | 14MC0513 | Scott Fleming | 05/08/2021 | 9 | 0 | 0 |
| 93 | 911BD813528546089A3102CDB5F5BE26 | 08/08/2021 | 14MC0513 | Scott Fleming | 06/08/2021 | 9 | 0 | 0 |
| 93 | 911BD813528546089A3102CDB5F5BE26 | 08/08/2021 | 14MC0513 | Scott Fleming | 07/08/2021 | 0 | 0 | 0 |
| 93 | 911BD813528546089A3102CDB5F5BE26 | 08/08/2021 | 14MC0513 | Scott Fleming | 08/08/2021 | 0 | 0 | 0 |
| 100 | 0A186D8CDB6B499C8BFEBF104C023C6C | 15/08/2021 | 14MC1136 | Adrian Baxanean | 09/08/2021 | 9 | 2 | 0 |
| 100 | 0A186D8CDB6B499C8BFEBF104C023C6C | 15/08/2021 | 14MC1136 | Adrian Baxanean | 10/08/2021 | 9 | 2 | 0 |
| 100 | 0A186D8CDB6B499C8BFEBF104C023C6C | 15/08/2021 | 14MC1136 | Adrian Baxanean | 11/08/2021 | 9 | 0 | 0 |
| 100 | 0A186D8CDB6B499C8BFEBF104C023C6C | 15/08/2021 | 14MC1136 | Adrian Baxanean | 12/08/2021 | 9 | 0 | 0 |
| 100 | 0A186D8CDB6B499C8BFEBF104C023C6C | 15/08/2021 | 14MC1136 | Adrian Baxanean | 13/08/2021 | 9 | 0 | 0 |
| 100 | 0A186D8CDB6B499C8BFEBF104C023C6C | 15/08/2021 | 14MC1136 | Adrian Baxanean | 14/08/2021 | 0 | 0 | 0 |
| 100 | 0A186D8CDB6B499C8BFEBF104C023C6C | 15/08/2021 | 14MC1136 | Adrian Baxanean | 15/08/2021 | 0 | 0 | 0 |
+----------+----------------------------------+------------+----------+-------------------+------------+--------+---------+---------+
Here is create and sample data:
CREATE TABLE #TimeSheets (
[RECORDER] [numeric](18, 0) NULL,
[CIRECID] [varchar](32) NULL,
[ENDDATE] [datetime] NULL,
[ITEMNO] [varchar](20) NULL,
[MEMO] [varchar](max) NULL,
[STDHRS#1] [decimal](15, 4) NULL,
[STDHRS#2] [decimal](15, 4) NULL,
[STDHRS#3] [decimal](15, 4) NULL,
[STDHRS#4] [decimal](15, 4) NULL,
[STDHRS#5] [decimal](15, 4) NULL,
[STDHRS#6] [decimal](15, 4) NULL,
[STDHRS#7] [decimal](15, 4) NULL,
[OTHRS#1#1] [decimal](15, 4) NULL,
[OTHRS#2#1] [decimal](15, 4) NULL
);
INSERT INTO #timesheets VALUES (91,'0A186D8CDB6B499C8BFEBF104C023C6C','2021-08-08','14MC1136','Adrian Baxanean',9,9,9,9,9,0,0,2,1);
INSERT INTO #timesheets VALUES (93,'911BD813528546089A3102CDB5F5BE26','2021-08-08','14MC0513','Scott Fleming',9,9,9,9,9,0,0,1,1);
INSERT INTO #timesheets VALUES (100,'0A186D8CDB6B499C8BFEBF104C023C6C','2021-08-15','14MC1136','Adrian Baxanean',9,9,9,9,9,0,0,2,2);
SELECT * FROM #timesheets
How can I accomplish this? I had thought to use UNPIVOT but couldn't see that would give me what I need. Any help would be appreciated.
Many thanks.