This looks like what I need as well. However, not being proficient in query making sense out of some of this.. so can you please clarify some things for me?
;WITH x AS <--- is this a comment?
( SELECT *, rn = ROW_NUMBER() OVER (PARTITION BY ID, [Type] ORDER BY [Contact No]) FROM dbo.SourceTable )
SELECT ID, HPhone = MAX(CASE WHEN [Type] = 'home' THEN [Contact No] END),
CPhone1 = MAX(CASE WHEN [Type] = 'cell' AND rn = 1 THEN [Contact No] END),
CPhone2 = MAX(CASE WHEN [Type] = 'cell' AND rn = 2 THEN [Contact No] END),
WPhone1 = MAX(CASE WHEN [Type] = 'work' AND rn = 1 THEN [Contact No] END)
, Ext1 = MAX(CASE WHEN [Type] = 'work' AND rn = 1 THEN [Ext] END),
WPhone2 = MAX(CASE WHEN [Type] = 'work' AND rn = 2 THEN [Contact No] END),
Ext2 = MAX(CASE WHEN [Type] = 'work' AND rn = 2 THEN [Ext] END)
FROM x <---------- what is 'x'? GROUP BY ID ORDER BY ID;
I copied the query and pasted it here from https://dba.stackexchange.com/a/242195