6
WITH list_dedup (Company, duplicate_count) AS
(
     SELECT
         *,
         ROW_NUMBER() OVER (PARTITION BY Company ORDER BY Email) AS 'RowNumber'
     FROM
         Travels
)

Error:

Msg 102, Level 15, State 1, Line 7
Incorrect syntax near ')'.

2
  • 1
    I think the number (and types) of columns in the CTE must match what you select in the actual query. Commented Oct 16, 2016 at 16:20
  • You need to have a statement using the CTE after the definition. e.g. WITH list_dedup (Company, duplicate_count) AS ( ... ) SELECT * FROM list_dedup Commented Oct 16, 2016 at 16:25

3 Answers 3

10

You are missing a final select for the common table expression (after the definition of the CTE):

WITH list_dedup  (Company,duplicate_count) As
(
  select *,
         ROW_NUMBER() OVER (PARTITION BY Company ORDER by Email) As "RowNumber"
  From Travels
)
select *  
from list_dedup;

But this will not because the CTE is defined to have two columns (through the WITH list_dedup (Company,duplicate_count)) but your select inside the CTE returns at least three columns (company, email, rownumber). You need to either adjust the column definition for the CTE, or leave it out completely:

WITH list_dedup As
(
  select *,
         ROW_NUMBER() OVER (PARTITION BY Company ORDER by Email) As "RowNumber"
  From Travels
)
select *  
from list_dedup;

The As "RowNumber" in the inner select also doesn't make sense when the column list is defined, because then the CTE definition defines the column names. Any alias used inside the CTE will not be visible outside of it (if the CTE columns are specified in the with .. (...) as part).

Sign up to request clarification or add additional context in comments.

Comments

4

You've just set up your CTE - now you need to use it!

WITH list_dedup (Company, duplicate_count) AS
(
     SELECT
         *,
         ROW_NUMBER() OVER (PARTITION BY Company ORDER BY Email) AS 'RowNumber'
     FROM
         Travels
)
SELECT *
FROM list_dedup

Comments

0

With marc_s's answer, you may also need a ;

;WITH list_dedup (Company, duplicate_count) AS
(
     SELECT
         *,
         ROW_NUMBER() OVER (PARTITION BY Company ORDER BY Email) AS 'RowNumber'
     FROM
         Travels
)

SELECT * FROM list_dedup

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.