sql - select record for recent n years -


how select records 3 recent years, considering year instead of whole date dd-mm-yy.
let's year 2013, query should display records 2013,2012 , 2011.
if next year 2014, records shall in 2014, 2013, 2012
, on....

table foo    id    date ----- --------- a0001 20-may-10 a0002 20-may-10 a0003 11-may-11 a0004 11-may-11 a0005 12-may-11 a0006 12-jun-12 a0007 12-jun-12 a0008 12-jun-12 a0009 12-jun-13 a0010 12-jun-13 

i think clearest way use year() function:

select * t year("date") >= 2011; 

however, use of function in where clause prevents index being used. so, if have large amount of data , index on date column, uses current date better. following takes approach:

select * t "date" >= trunc(add_months(sysdate, -3*12), 'yyyy') 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -