Environment: SQL Server 2008 R2
Problem: How I would unpivot the following table.
Code:
CREATE TABLE [dbo].[ind_subject_scores]( [ind_scr_id] [int] IDENTITY(1,1) NOT NULL, [id] [char](9) NULL, [subject_cd] [char](2) NULL, [score] [varchar](2) NULL, [test_dt] datetime, skill_id int, form_cd char(1), raw_scr int ) INSERT INTO [dbo].[ind_subject_scores] ( id ,Test_dt ,Score ,Subject_cd , skill_id , form_cd , raw_scr ) VALUES ( 897841239, '20110101' ,'2' ,'FR' ,1 ,'A' ,55 ); INSERT INTO [dbo].[ind_subject_scores] ( id ,Test_dt ,Score ,Subject_cd , skill_id , form_cd , raw_scr ) VALUES ( 897841239, '20110101' ,'2' ,'FR' ,2 ,'A' ,45 ); INSERT INTO [dbo].[ind_subject_scores] ( id ,Test_dt ,Score ,Subject_cd , skill_id , form_cd , raw_scr ) VALUES ( 80041239, '20110115' ,'2' ,'CH' ,1 ,'A' ,45 ); INSERT INTO [dbo].[ind_subject_scores] ( id ,Test_dt ,Score ,Subject_cd , skill_id , form_cd , raw_scr ) VALUES ( 80041239, '20110115' ,'2' ,'CH' ,2 ,'A' ,49 );
Skill_id has two values i= listening and 2=reading. I would like to display data by two columns listening and reading associated with raw_sc and form_cd. The expected result should show:
Id | Test_dt| Subject_cd | Listening | Reading | form_cd
1 | 20110101 | FR | 55 | 45 | A