Skip to main content
October 30, 2006
Question

Displaying top X results?

  • October 30, 2006
  • 3 replies
  • 529 views
I'm brand-new at a job, and working my way through the CF site, so I apologize in advance for sounding nebulous. Everything in the site uses FuseActions and stored procedures, and then pulls specific query language into those procedures.

I've got a query that's returning the desired results, except that we only care about the last 3 years of data. I'm totally blanking on the code string to use to limit this -- I'd prefer to use something dynamic, rather than having to hardcode in a value. I've tried a WHERE YearField IN (Max(YearField), Max(YearField) -1, Max(YearField)-2) and it throws up because it's an aggregate statement. Putting in a GROUP BY didn't help. So I tried a HAVING using the same logic, to no avail.

(FWIW, I've tried it both Max(YearField) - '1' and Max(YearField) -1.)

Is there a TOP 3 type function, or does anyone else have a better idea?
This topic has been closed for replies.

3 replies

tclaremont
Inspiring
October 30, 2006
I would try:

where year(DateField) >= (#year(Now())# - 3)
Inspiring
October 30, 2006
Did you try

where yearField > #currentYear#-3, although thats only approximately 3 years.

If the row is datestamped then some kind of: -
where dateStamp > sysDate-(365*3) if it's oracle,
where dateStamp > DATEADD(YEAR,-3,getDate()) if it's MSSQL
should do it..
Inspiring
October 30, 2006
My favourite way to do this is with db date functions. Those are db specific of course.

Another way is to use cold fusion date functions to create a variable, and use that variable in your query.

Whether or not your db has a top 3 function depends on the software you are using.
October 30, 2006
I'm using SQL Server. And you can bet I'm digging out that SQL book to bring to the office tomorrow! :)