I have a table:
CREATE TABLE StatusStats (
UserID VARCHAR(50),
StatusCategory VARCHAR(50),
StatusDuration INT
)
That contains, for each user, how much time they were (StatusCategory)
- Productive
- NonProductive
- Unknown
(fyi… status duration is in seconds)
Those are the only 3 values.
In order to use this data in reporting using JOINs to other tables, I need to copy that data in to this table:
CREATE TABLE StatusStatsByUser (
UserID VARCHAR(50),
Productive INT,
NonProductive INT,
Unknown INT
)
That will contain in each column the value that was in the StatusDuration column for the corresponding value in the StatusCategory column.
I can’t figure out how to write the SQL code to do that, using SQL Server 2008R2. Your help is very much appreciated.
There is no need to store this data in a table, you can easily create a view that
PIVOTs the data into the columns that you need:Once this is in a view, then you can join on it with your other tables:
But if you must insert this into a new table, you can use: