question

renugsk avatar image
renugsk asked

DYNAMIC COLUMNS IN SQL SERVER 2008

DATE CODE STATUS

01/01/2010 1111 AVAILABLE
01/01/2010 2222 NOT AVAILABLE
01/01/2010 3333 AVAILABLE
01/02/2010 1111 AVAILABLE
01/02/2010 2222 NOT AVAILABLE
01/02/2010 3333 NOT AVAILABLE

I NEED TO CHANGE THIS LIKE BELOW

DATE 1111 2222 3333

01/01/2010 AVAILABLE NOTAVAILABLE AVAILBALE

01/02/2010 AVAILABLE NOTAVAILABLE NOTAVAILBALE

sql-server-2008
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Squirrel 1 avatar image
Squirrel 1 answered
declare @sample table            
(            
    DATE 	date,            
    CODE 	int,            
    STATUS	varchar(20)            
)            
            
insert into @sample            
select  '01/01/2010', 1111, 'AVAILABLE'		union all            
select  '01/01/2010', 2222, 'NOT AVAILABLE'	union all            
select  '01/01/2010', 3333, 'AVAILABLE'		union all            
select  '01/02/2010', 1111, 'AVAILABLE'		union all            
select  '01/02/2010', 2222, 'NOT AVAILABLE'	union all            
select  '01/02/2010', 3333, 'NOT AVAILABLE'            
            
-- Query                
select  [DATE], [1111], [2222], [3333]            
from    @sample            
    pivot            
    (            
    	max(STATUS)            
    	for CODE in ([1111], [2222], [3333])            
    ) p            
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Madhivanan avatar image
Madhivanan answered
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.