I can’t figure out how I would get the field names into data in a row. The pivot function only takes data from the table (which makes sense, but doesn’t help me)
That’s what I am trying to do. Looking at it now, I don’t know if the pivot function is the right tool for the job. I’m trying to see if I can get a CTE to work now buy looping through 1-7 and doing a calculated field with a case statement to grab correct field. I just really don’t understand CTE queries. I’ve gotten a couple of them to work before, but I don’t understand how/why some things work the way they do, so unless I have an explicit example, they never seem to work for me.
I think I may have figured it out. (It still seems like trial and error to me) but I have a list of number and calendar ID’s so I should be able to make my calculated field.
with [DayNumbers] as
(select
[ProdCal].[Company] as [ProdCal_Company],
[ProdCal].[CalendarID] as [ProdCal_CalendarID],
(1) as [Calculated_DayNum]
from Erp.ProdCal as ProdCal
union all
select
[ProdCal1].[Company] as [ProdCal1_Company],
[ProdCal1].[CalendarID] as [ProdCal1_CalendarID],
(DayNumbers.Calculated_DayNum + 1) as [Calculated_DayNum]
from DayNumbers as DayNumbers
inner join Erp.ProdCal as ProdCal1 on
DayNumbers.ProdCal_CalendarID = ProdCal1.CalendarID
and DayNumbers.ProdCal_Company = ProdCal1.Company
where DayNumbers.Calculated_DayNum <= 6)
select
[DayNumbers1].[ProdCal_Company] as [ProdCal_Company],
[DayNumbers1].[ProdCal_CalendarID] as [ProdCal_CalendarID],
[DayNumbers1].[Calculated_DayNum] as [Calculated_DayNum],
(case when DayNumbers1.Calculated_DayNum = 1 then ProdCal2.WorkWeek1
when DayNumbers1.Calculated_DayNum = 2 then ProdCal2.WorkWeek2
when DayNumbers1.Calculated_DayNum = 3 then ProdCal2.WorkWeek3
when DayNumbers1.Calculated_DayNum = 4 then ProdCal2.WorkWeek4
when DayNumbers1.Calculated_DayNum = 5 then ProdCal2.WorkWeek5
when DayNumbers1.Calculated_DayNum = 6 then ProdCal2.WorkWeek6
when DayNumbers1.Calculated_DayNum = 7 then ProdCal2.WorkWeek7
else 0
end) as [Calculated_ValidDay]
from DayNumbers as DayNumbers1
inner join Erp.ProdCal as ProdCal2 on
DayNumbers1.ProdCal_Company = ProdCal2.Company
and DayNumbers1.ProdCal_CalendarID = ProdCal2.CalendarID