sql server - How to find last 10 years using sql query -


i working on sql query have find last 10 years. suppose 2015 then query should return 2015,2014,2013... , on. have used following query-

select top 10 datepart(year,getdate()) order datepart(year,getdate()) desc 

but above query returning single query current year. please me here.

try this:

with yearlist  (     select (datepart(year,getdate())-10) year     union     select yl.year + 1 year     yearlist yl     yl.year + 1 <= year(getdate()) )  select year yearlist order year desc; 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -