tags:

views:

79

answers:

4
+2  Q: 

rownum in Subquery

I was wondring why this doesn't work:

SELECT 
  (
    SELECT COALESCE(MAX(a.LaufNr),0) + TBL.Rownum
    FROM schemaB.PersonRelatedTbl A
    WHERE A.Persid = Tbl.Persid
  )
  , Tbl.Some_Other_Attributs
FROM schemaA.PersonRelatedTbl TBL 
WHERE ...

This + TBL.Rownum gives an error
why?

greets Auro

+1  A: 

The TBL alias isn't used in the sub query but in the main query. ie

SELECT COALESCE(MAX(a.LaufNr),0) + TBL.Rownum
FROM schemaB.PersonRelatedTbl A
WHERE A.Persid = Tbl.Persid

is not a valid query.

Dave Barker
well i think thats not the problem :D coze i have Tbl.Persidand it works fine
Auro
A: 

Row num is per output row - not for every table

Why not try

SELECT 
  (
    SELECT COALESCE(MAX(a.LaufNr),0)
    FROM schemaB.PersonRelatedTbl A
    WHERE A.Persid = Tbl.Persid
  ) || rownum, Some_Other_Attributs
FROM schemaA.PersonRelatedTbl TBL 
WHERE ...
josephj1989
doesnt || neans just ad behind? like 1 || 2 = 12 ??
Auro
Yes - it's a shortcut for string concatentation - so it automatically converts the type on either side to a string.Also you would still have the issue that ROWNUM would have a arbitrary value.
JulesLt
+3  A: 

rownum is a pseudocolumn in a result set. It is not associated with any tables, and is the among the last things that get assigned (it happens after all sorting, etc).

It is possible that the row_number() function would be more useful for what you're doing, but, if you want the numbers to stay the same across all query invocations for each given row then you're going to have to store the numbers in the database (or use rowid, but, that's more like a serial number and is pretty ugly to show to end users). It's not really clear from your example what you expect rownum to be doing.

Donnie
nah its not a column in my Table.i have a table where a person can have more then one row something like an history. I need to Merge 2 tables (same attributs etc) and when i uses this script(its an insert/select) i dont know the actual row number. soi wanted to read the actual highst of the user and start adding at this number (e.g. 5 is the hist now then i want to start adding with 6 and so on...)
Auro
'(it happens after all sorting, etc)' this may be the rease why it dosent work. Thx!
Auro
It gets assigned before sorting, which can cause some people problems.SELECT numval from big_table_of_numbers WHERE ROWNUM < 3 ORDER BY numval DESC will get 2 numbers from the table, and then order them in descending order, rather than the last 2 numbers.Which 2 numbers it gets is up to the database.You need to do SELECT * FROM (SELECT numval FROM blah ORDER BY numval DESC) WHERE rownum < 2
JulesLt
A: 

Reading between the lines in the comments above, and your SQL, I would suggest reading up on Oracle's analytic functions.

http://www.orafaq.com/node/55

These are excellent for calculating things that typically need nested sub-queries that perform group functions - i.e. 'give me the running balance of X for each row', 'give me the rank of X out of the total set of data' or simply 'give me the total count of data'.

What makes them hard to grasp at first is that the syntax is complex, as they can do similar queries for partitions of your data set.

In your case you could probably use the COUNT(*) OVER () operation to get the 'height' from your other table.

JulesLt