简体   繁体   中英

Selecting filtered values from Oracle using ROWNUM

I have a requirement wherein i need to find the record number of the records that are returned from the resultset. I know that i can use ROWNUM to get the record number from the resultset but my issue is slightly different. below are the details

Table : ProcessSummary Columns:

PS_PK ProcessId StepId AsscoiateId ProcessName AssetAmount
145     25        50                 Process1    3,500.00
267     26               45          Process2    4,400.00
356     27        70                 Process3    2,400.00
456     28        80     90          Process4      780.00   
556     29        56     67          Process5    4,500.00
656               45     70          Process6    6,000.00
789     31               75          Process7    8,000.00

Now what i need to do is fetch all the records from the ProcessSummary Table when either of ProcessId OR StepId OR AssociateId is NULL. I wrote the below query

select * from  ProcessSummary where ProcessId IS NULL OR StepId IS NULL OR AsscoiateId IS NULL

As expected i got 1st, 2nd, 3rd, 6th and 7th records in the resultset that got returned. Now what i need is to get the records numbers 1,2,3,6,7. I tried to use the ROWNUM as below but i got the values of 1,2,3,4,5 and not 1,2,3,6,7.

select ROWNUM from  ProcessSummary where ProcessId IS NULL OR StepId IS NULL OR AsscoiateId IS NULL

Is it possible to get the ROWNUM values in the sequence that i want and if yes then can you please let me know how can i do this. Also if ROWNUM cannot be used then what would be the other option that i can use to get the result in the form that i want.

Any help would be greately appericiated as i could not find much on the net or SO regarding this sort of requirement.

Thanks

Vikeng21

rownum is an internal numbering that gives you a row number based on the current query results only, so that numbering is not tied to a specific record, and it will change when you change the data or the query.

But the numbering you ask for is already in your table. It looks like you just need to SELECT PS_PK .. instead. PS_PK is the field in your table that contains the actual number you want.

You can generate a numbering using an analytical function, and then filter that query. You need some fields to order by, though. In this case I've chosen PS_PK , but it can be another field, like ProcessName or a combination of other fields as well.

select
  *
from
  (select
    dense_rank() over (order by PS_PK) as RANKING,
    p.*
  from
    ProcessSummary p)
where
  ProcessId IS NULL OR StepId IS NULL OR AsscoiateId IS NULL

So, in this query, first a numbering is calculated for each row that is returned from the inner query. The numbering is returned as the field RANKING. And then the other query filters further, but still will return the field RANKING with the original numbering.

Instead of dense_rank there is also rank and row_number . The differences are subtle, but you can just experiment and read some docs here and here to learn about the differences and see which one fits you best.

Note that this might slow down your query, because the inner query first generates a number for each row in the table (there is no filtering on that level now).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM