I have some problems with a SQL query, I try to convert row in columns.
select U.FirstName,U.LastName,Sh.[Day],Sh.StartTime,Sh.EndTime from dbo.[User] as U
inner join dbo.Employee as E on U.UserId = E.UserId
inner join dbo.ScheduleStaff as SH on SH.EmployeeId = E.EmployeeId
where ((Sh.[Day] < getdate()+ 4 ) and sh.[Day] >= getdate())
group by U.FirstName, U.LastName,Sh.[Day],Sh.StartTime,Sh.EndTime
This query gives me the below result:
How can I make that there is one employee per line, and the days turn into columns?
I try this:
select FirstName,LastName,Day1,Day2,Day3
from
(
select U.FirstName,U.LastName,Sh.[Day],Sh.StartTime,Sh.EndTime,concat('Day',ROW_NUMBER() over (partition by
U.FirstName,U.LastName order by U.FirstName,U.LastName)) as Tests
from dbo.[User] as U
inner join dbo.Employee as E on U.UserId = E.UserId
inner join dbo.ScheduleStaff as SH on SH.EmployeeId = E.EmployeeId
where ((Sh.[Day] < getdate()+ 4 ) and sh.[Day] >= getdate()-1)
group by U.FirstName, U.LastName,Sh.[Day],Sh.StartTime,Sh.EndTime
)Temp
pivot
(
Max([Day])
for Tests in (Day1,Day2,Day3)
)Piv
But this is not exactly what I need, how can I make the days as columns and their time as values?