Need little help here, trying to write an SQL that do as follows
have two tables and when I join them I would get something like
Column A---Column B
New York----David
New York----John
New York----Alex
Atlanta-------Chris
Florida-------Phil
L.A-----------David
L.A-----------Jenny
L.A-----------Boris
L.A-----------Joseph
Miami--------Alex
Miami--------Will
Now I want the query to return just a limited rows using rownum, for example
where rownum <= 3
However this would return
Column A---Column B
New York----David
New York----John
New York----Alex
ONLY
But I need all results of first column <= 3, what i am saying is that I need the query to return me
Column A---Column B
New York----David
New York----John
New York----Alex
Atlanta-------Chris
Florida-------Phil
where column A has 3 values
is this possible at all? I have seek a few ways but cant do it!!!
I don't have any database handy to test my SQLs, but I just looked at SQL manual and found out that you can only use analytical functions in the SELECT LIST and in ORDER BY clause of a query, not in the WHERE clause. So you have to use inline views. For joining two tables and showing only up to 3 names for each city, you would use:
SELECT colA, colB FROM
(SELECT t1.colA, t2.colB, ROW_NUMBER() OVER (PARTITION BY column_a) row_num_for_colA
FROM t1, t2 WHERE .....)
WHERE row_num_for_colA <= 3;
Jurij Modic ASCII a stupid question, get a stupid ANSI
24 hours in a day .... 24 beer in a case .... coincidence?
Is that partition compulsory...? The tables I am querying arent partitioned... I will have a look at documentation as well, did you find this in the SQL guide?
In analytical functions, PARTITION BY have nothing to do with table partitions (or index partitions). It merely defines on what range the analytical function is aplied. In this case, a distinct city name reperesents a partition and on each of this partitions each rows gets sequential row number (starting with 1 at each new "partition"). And yes, these new analytical functions are explained in SQL manual (from 8.1.6 onwards).
Jurij Modic ASCII a stupid question, get a stupid ANSI
24 hours in a day .... 24 beer in a case .... coincidence?
(It sorts it into order when you create the temp table using select distinct)
Hope this helps.
If not I'd be interested to see how the problem is solved. I'd appreciate it if you could E-Mail the solution to cmoffat99@yahoo.co.uk - It's got me a little bugged on how you can get the exact result you asked for - unsorted.
No that wouldnt work, the example I posted is a simplified version what we are trying to do in a search engine (thatīs why I have to limit the result set using rownum) If I start to create temporary tables it would be one temporary table for each search since the conditions in the search can vary quite a lot...
Bookmarks