I’m using the ROW_NUMBER() function in SQL Server to create a new column called RowNumber.
But when I later in the same query reference this new field in the WHERE statement I get this error, even though the field should be an integer:
Conversion failed when converting the varchar value ‘RowNumber’ to data type int
Here is my query:
SELECT
ROW_NUMBER() OVER(PARTITION BY c.Node_base ORDER BY sum(c.Score) DESC) AS "RowNumber",
c.Node_base, c.Node_forslag, sum(Score)
FROM
t_input as c
WHERE 'RowNumber' < 11
GROUP BY c.Node_base, c.Node_forslag
ORDER BY c.Node_base desc
You can’t reference a calculated column from the
SELECTclause in theWHEREclause – and even if you could, single quotes introduce a string literal, rather than a column reference. Use a CTE or subquery.There’s no need to quote the name
RowNumberat all – it’s not a reserved word, and it contains no special characters.