-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

0

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.