I have two tables defined as the following:
User (ID int, name varchar)
Hours (UserID int, [date] date, hours float)
Now i can perform a join to get the number of hours for each person like so:
SELECT U.ID, U.Name, H.[date], H.Hours
FROM Users U
LEFT JOIN Hours H ON U.ID = H.UserID
WHERE H.Date > '2011-01-01'
AND H.Date < '2011-02-01'
Which would give me a result set with the following columns (and between the date range):
ID, Name, Date, Hours
What I would like to do is change the output columns so that it appears more like a spreadsheet:
ID, Name, 2011-01-01, 2011-01-02, 2011-01-03 ..... 2011-01-31
and the corresponding columns with the correct hour values.
Is this possible?
Well, you are gonna need to use dynamic sql for that, if you want to change the date ranges. So first, take a look to this link. Then you can try the following: