Complicated Top-N Query
653482Aug 4 2008 — edited Aug 5 2008Okay, so I've been a good boy and ran a search for my particular issue beforehand. It still hasn't helped.
Here's the problem: I have a rather complicated query to run (I'm calling it from Flex via BlazeDS, so basically it's being called from Java). The columns are, in order: a 1 (yes, all 1s), a date, the sum of six columns, the sum of two other columns, the sum of a single column, and the sum of a bunch of columns divided and multiplied so it comes out as a percentage (I won't walk you through the syntax, it's kinda hairy). Yeeah, kind of a pain. It works (I was actually given this to try and work with), but I'm trying to introduce pagination or some kind of limitation on the number of rows to keep the processing time down.
So, I found out this kind of problem is referred to as a Top-N Query, and lots of people have dealt with it before (17 pages worth of it!). I thought I'd try what some people had been using for a solution. Unfortunately, nothing I've tried seems to work. I tried the first method listed at http://www.oracle.com/technology/oramag/oracle/07-jan/o17asktom.html, inserting my query where (your_query) sat and changing nothing else, and got the following error message: java.sql.SQLSyntaxErrorException : ORA-00937: not a single-group group function.
I then tried the second 'working' method on that page, where row_number() is used. Again I followed the instructions exactly as written, except selecting my own columns and table, and ordering by date, and got the same error as before. This post is already getting pretty long so suffice to say I tried a few forum-posted solutions and got nada.
What I'd like to do here is only retrieve the first 8 dates in the database. Just to make sure it wasn't something to do with my environment I put the original query back in (where it pulls everything), and it works albeit slowly...so the problem seems to be somewhere in the Oracle syntax.
For reference purposes, here's the original query (table and column names have been made generic): SELECT 1 a, date, (SUM(b) + SUM(c) + SUM(d) + SUM(e) + SUM(f) + SUM(g)) sum1, SUM(h) + SUM(i) sum2, SUM(j) sum3, ROUND(100 -(((SUM(b) /(SUM(b) + SUM(c) + SUM(d) + SUM(e) + SUM(f) + SUM(g)))) *100), 2) percent FROM table GROUP BY date ORDER BY date ASC