I have below table
| Monday | Tuesday | Wednesday | Thursday | Friday | WeekStartsOn |
| ------------ | ------------ | ------------ | ------------ | ------------ | ------------ |
| 8 | 8 | 8 | 8 | 8 | 2019-09-06 |
I want to convert it to below table
DayOftheWeek | ActualHrs | WeekStartsOn | TimecardDate |
---|---|---|---|
Monday | 8 | 2019-09-06 | 2019-09-07 |
Tuesday | 8 | 2019-09-06 | 2019-09-08 |
Wednesday | 8 | 2019-09-06 | 2019-09-09 |
Thursday | 8 | 2019-09-06 | 2019-09-10 |
Friday | 8 | 2019-09-06 | 2019-09-11 |
The logic To add **TimecardDate **is for Monday-->WeekstartsOn+1, Tuesday--> WeekStartsOn+2 .... , Friday--> WeekstartsOn+5
I have tried below Query
SELECT ActualHrs,DayOftheWeek,WeekStartsOn
FROM (select Monday,Tuesday,Wednesday,Thursday,Friday,WeekStartsOn
from itplanning.enriched.timecard_enriched_current) as cp
UNPIVOT
(
ActualHrs FOR DayOftheWeek IN (Monday,Tuesday,Wednesday,Thursday,Friday )
)
AS PivotTable
where ActualHrs<>0
with this Above query, I have got below table
DayOftheWeek | ActualHrs | WeekStartsOn |
---|---|---|
Monday | 8 | 2019-09-06 |
Tuesday | 8 | 2019-09-06 |
Wednesday | 8 | 2019-09-06 |
Thursday | 8 | 2019-09-06 |
Friday | 8 | 2019-09-06 |
I am unable to do the TimecardDate column.
Actual table is below
| Monday | Tuesday | Wednesday | Thursday | Friday | WeekStartsOn |
| ------------ | ------------ | ------------ | ------------ | ------------ | ------------ |
| 8 | 8 | 8 | 8 | 8 | 2019-09-06 |
Expected Table is below
DayOftheWeek | ActualHrs | WeekStartsOn | TimecardDate |
---|---|---|---|
Monday | 8 | 2019-09-06 | 2019-09-07 |
Tuesday | 8 | 2019-09-06 | 2019-09-08 |
Wednesday | 8 | 2019-09-06 | 2019-09-09 |
Thursday | 8 | 2019-09-06 | 2019-09-10 |
Friday | 8 | 2019-09-06 | 2019-09-11 |
1条答案
按热度按时间wfsdck301#
You can do something like this instead:
I unpivot manually and creates a two column table containing week and how many days to add to start of the week