We have a table salarymonth which defines the range of dates for each salary month.
Our month goes from 26 to 25.
For January 2021 and February 2021 it is like.
insert @salarymonth (yearmonth, dt1, dt2)
values (202101,'2020/12/26','2021/01/25')
, (202102,'2021/01/26','2021/02/25')
select * from @salarymonth
yearmonth | dt1 | dt2 |
---|---|---|
202101 | 2020-12-26 | 2021-01-25 |
202102 | 2021-01-26 | 2021-02-25 |
Due to some porblem , I am finding one restday for each employee for each month
from transactions table in a CTE (reverse engineering).
One record for each employee for each month.
Let's hold the findings in a table variable.
insert @restdays (emp_code, yearmonth, dt,dn)
values ('000042',202101,'2021/01/06','Wednesday')
, ('000042',202102,'2021/01/27','Wednesday')
, ('000062',202101,'2020/12/29','Tuesday')
, ('000062',202102,'2021/02/16','Tuesday')
select * from @restdays
emp_code | yearmonth | dt | dn |
---|---|---|---|
000042 | 202101 | 2021-01-06 | Wednesday |
000042 | 202102 | 2021-01-27 | Wednesday |
000062 | 202101 | 2020-12-29 | Tuesday |
000062 | 202102 | 2021-02-16 | Tuesday |
Now we need to create rows for all rest day dates (+- 7days)
in a month for each employee.
The final result should be
insert @finalresult (emp_code , yearmonth, dt, dn)
values ('000042',202101,'2020/12/30','Wednesday')
, ('000042',202101,'2021/01/06','Wednesday')
, ('000042',202101,'2021/01/13','Wednesday')
, ('000042',202101,'2021/01/20','Wednesday')
, ('000042',202102,'2021/01/27','Wednesday')
, ('000042',202102,'2021/02/03','Wednesday')
, ('000042',202102,'2021/02/10','Wednesday')
, ('000042',202102,'2021/02/17','Wednesday')
, ('000042',202102,'2021/02/24','Wednesday')
insert @finalresult (emp_code , yearmonth, dt, dn)
values ('000062',202101,'2020/12/29','Tuesday')
, ('000062',202101,'2021/01/05','Tuesday')
, ('000062',202101,'2021/01/12','Tuesday')
, ('000062',202101,'2021/01/19','Tuesday')
, ('000062',202102,'2021/01/26','Tuesday')
, ('000062',202102,'2021/02/02','Tuesday')
, ('000062',202102,'2021/02/09','Tuesday')
, ('000062',202102,'2021/02/16','Tuesday')
, ('000062',202102,'2021/02/23','Tuesday')
select * from @finalresult
emp_code | yearmonth | dt | dn |
---|---|---|---|
000042 | 202101 | 2020-12-30 | Wednesday |
000042 | 202101 | 2021-01-06 | Wednesday |
000042 | 202101 | 2021-01-13 | Wednesday |
000042 | 202101 | 2021-01-20 | Wednesday |
000042 | 202102 | 2021-01-27 | Wednesday |
000042 | 202102 | 2021-02-03 | Wednesday |
000042 | 202102 | 2021-02-10 | Wednesday |
000042 | 202102 | 2021-02-17 | Wednesday |
000042 | 202102 | 2021-02-24 | Wednesday |
000062 | 202101 | 2020-12-29 | Tuesday |
000062 | 202101 | 2021-01-05 | Tuesday |
000062 | 202101 | 2021-01-12 | Tuesday |
000062 | 202101 | 2021-01-19 | Tuesday |
000062 | 202102 | 2021-01-26 | Tuesday |
000062 | 202102 | 2021-02-02 | Tuesday |
000062 | 202102 | 2021-02-09 | Tuesday |
000062 | 202102 | 2021-02-16 | Tuesday |
000062 | 202102 | 2021-02-23 | Tuesday |
How to get @finalresult via tsql from @salarymonth, @restdays
I want it in SQL 2008 (I am junior, to upgrade machines and SQL not in my hand)
Regards