-4

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

Paul White
  • 94,921
  • 30
  • 437
  • 687
Detox
  • 1
  • 1

1 Answers1

5

This is the SQL syntax for a common table expression (CTE). It defines a "virtual table" named x, from which you can later select.

The semicolon that precedes WITH is a quirk explained here.

ypercubeᵀᴹ
  • 99,450
  • 13
  • 217
  • 306
mustaccio
  • 28,207
  • 24
  • 60
  • 76