CREATE TABLE [dbo].[ashish](
[custID] [int] NULL,
[Question] [varchar](50) NULL,
[answer] [varchar](20) NULL
) ON [PRIMARY]
INSERT INTO ashish values(1000, 'AAA', '1')
INSERT INTO ashish values(1000, 'BBB B', '2')
INSERT INTO ashish values(1000, 'CCC', '3')
INSERT INTO ashish values(1001, 'AAA', '2')
INSERT INTO ashish values(1001, 'BBB B', '3')
INSERT INTO ashish values(1001, 'CCC', '3')
INSERT INTO ashish values(1000, 'DDD', '6')
Declare @t VARCHAR(10)
Declare @A VARCHAR(1000)
Declare @B VARCHAR(1000)
set @A='SELECT CustID'
SET @B='('
SELECT @A=@A+',['+Question+'] as [' +Question+']',@B=@B+'['+Question+'],' FROM (SELECT DISTINCT QUESTION FROM ashish) cur
-- removing last ',' from both <span class="IL_AD" id="IL_AD5">variables</span>
SET @B=SUBSTRING(@B,1,LEN(@B)-1)
SET @A=@A+ + ' FROM (SELECT CustID, Answer, Question FROM ashish) s PIVOT (max(answer) FOR QUESTION IN ' +@B+')) p ORDER BY [CUSTID]; '
exec(@A);
[custID] [int] NULL,
[Question] [varchar](50) NULL,
[answer] [varchar](20) NULL
) ON [PRIMARY]
INSERT INTO ashish values(1000, 'AAA', '1')
INSERT INTO ashish values(1000, 'BBB B', '2')
INSERT INTO ashish values(1000, 'CCC', '3')
INSERT INTO ashish values(1001, 'AAA', '2')
INSERT INTO ashish values(1001, 'BBB B', '3')
INSERT INTO ashish values(1001, 'CCC', '3')
INSERT INTO ashish values(1000, 'DDD', '6')
Declare @t VARCHAR(10)
Declare @A VARCHAR(1000)
Declare @B VARCHAR(1000)
set @A='SELECT CustID'
SET @B='('
SELECT @A=@A+',['+Question+'] as [' +Question+']',@B=@B+'['+Question+'],' FROM (SELECT DISTINCT QUESTION FROM ashish) cur
-- removing last ',' from both <span class="IL_AD" id="IL_AD5">variables</span>
SET @B=SUBSTRING(@B,1,LEN(@B)-1)
SET @A=@A+ + ' FROM (SELECT CustID, Answer, Question FROM ashish) s PIVOT (max(answer) FOR QUESTION IN ' +@B+')) p ORDER BY [CUSTID]; '
exec(@A);
No comments:
Post a Comment