CREATE table #yourtable ([Id] int, [Value] varchar(6), [C...

0

Please login or register to vote for this query.

(click on this box to dismiss)

Physical Fitness

Q&A for physical fitness professionals, athletes, trainers, and those providing health-related needs

CREATE table #yourtable
    ([Id] int, [Value] varchar(6), [ColumnName] varchar(13))
;
    
INSERT INTO #yourtable
    ([Id], [Value], [ColumnName])
VALUES
  
    (1, 'Name1', 'Alarm1'),
    (2, 'Name2', 'Alarm2'),
    (3, 'Name3', 'Alarm3'),
    (1, 'Name11', 'Alarm1'),
    (2, 'Name22', 'Alarm2'),
    (2, 'Name33', 'Alarm2')
;


SELECT * FROM (
  SELECT
    [id],
    [value],
    [columnName]
  FROM #yourtable
) AlarmResults
PIVOT (
  SUM([id])
  FOR [Value]
  IN (
    [Alarm1],
    [Alarm2],
    [Alarm3]
  )
) AS PivotTable

Enter Parameters

Options:
Switch to meta site
loading Hold tight while we fetch your results