Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Row_Number() in SQLServer

Tags:

sql-server

select 
   sp_una_stl_key, 
   row_number() over(order by sp_una_stl_key)as stl_key 
from        
    t_unit_data_archive
where 
    stl_key>=10

This query is not executed, throws,

Msg 207, Level 16, State 1, Line 2 Invalid column name 'stl_key'.

i could not understand what is the problem. please help me!

like image 268
Partha Avatar asked Jul 05 '26 04:07

Partha


1 Answers

You can't use the ROW_NUMBER directly - you need to package it inside a Common Table Expression like this:

with CTE as
(
  select 
     sp_una_stl_key, row_number() over(order by sp_una_stl_key) as stl_key 
  from 
     t_unit_data_archive
)
select *
from CTE
where stl_key >= 10

Marc

like image 175
marc_s Avatar answered Jul 09 '26 19:07

marc_s



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!