-2

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
    [Please do not upload images of code/data/errors when asking a question.](//meta.stackoverflow.com/q/285551) – Thom A Apr 27 '23 at 16:46
  • please add your example data and expected. – abolfazl sadeghi Apr 27 '23 at 16:47
  • While asking a question, you need to provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example): (1) DDL and sample data population, i.e. CREATE table(s) plus INSERT T-SQL statements. (2) What you need to do, i.e. logic and your code attempt implementation of it in T-SQL. (3) Desired output, based on the sample data in the #1 above. (4) Your SQL Server version (SELECT @@version;). All within the question as text, no images. – Yitzhak Khabinsky Apr 27 '23 at 17:06

1 Answers1

0

You can do something like this instead:

SELECT  v.d,v.hours,DATEADD(DAY, v.addDay, WeekStartsOn), WeekStartsOn
FROM itplanning.enriched.timecard_enriched_current
CROSS APPLY (
  VALUES(Monday,'Monday', 1),(Tuesday,'Tuesday', 2),(Wednesday, 'Wednesday', 3),(Thursday, 'Thursday', 4),(Friday, 'Friday', 5)
 ) v(hours, d, addDay)
where hours<>0 

I unpivot manually and creates a two column table containing week and how many days to add to start of the week

Edit: added an alternative that does it with pivot syntax

select *
,   dateadd(day, case DayOftheWeek when 'monday' then 1 when 'tuesday' then 2 when 'wednesday' then 3 when 'thursday' then 4 when 'friday' then 5 end, weekstartson) AS TimecardDate
FROM itplanning.enriched.timecard_enriched_current
UNPIVOT 
( 
ActualHrs FOR DayOftheWeek IN (Monday,Tuesday,Wednesday,Thursday,Friday ) 
) 
AS PivotTable

Edit #3 (SQLite edition):

with cte as (
    select monday, 'monday', 1 AS addition, weekstartson
    from timecard_enriched_current
    union
    select tuesday, 'tuesday', 2, weekstartson
    from timecard_enriched_current
    union
    select wednesday, 'wednesday', 3, weekstartson
    from timecard_enriched_current
    union
    select thursday, 'thursday', 4, weekstartson
    from timecard_enriched_current
    union
    select friday, 'friday', 5, weekstartson
    from timecard_enriched_current
    ) 
select *, DATE(weekstartson, '+' || addition || ' days') AS TimecardDate
from cte
order by addition
siggemannen
  • 3,884
  • 2
  • 6
  • 24