Mamas Liebling
Mamas Liebling

Reputation: 25

Subquery Order by Oracle

My Database has 3 Tables, Device,Postal_Address and Company.

Every Device has an Company_ID, and for every company there is an address. The problem is, the old address is still there if i change it, its everytime making a new entry for this. So if i select all devices with address i get some entrys doubled.

select d.device,
 ( 
           select postalcode_address from 
           ( 
                select  
                     pa.postalcode_address,   
                     row_number() over (order by pa.last_update desc) r,
                     pa.company_id
                from   
                     postal_address pa   
           )  
             where  company_id=c.company_id
            AND r=1

      ) as Postcode
from device d,company c,
where d.company_id = c.company_id(+) 

I have tried it with order the address withlast_update, but its getting me nothing back, where is my error? so i need the newest entry in the postal_address for my company_id.

Upvotes: 0

Views: 47

Answers (1)

Alex Poole
Alex Poole

Reputation: 191580

Your row_number() analytic call is getting the row number across all companies, so you'll only get a match for the most-recently-changed company; no other company will get r = 1. You need to partition by the company ID:

row_number() over (partition by pa.company_id order by pa.last_update desc) r,

Or move the where company_id=c.company_id inside that nested subquery; but that doesn't work in some versions of Oracle (the c alias might not be visible).


I wouldn't use a subquery to get the column value within the select list at all here though; you could use the same row number check as an inline view and join to it:

select d.device, pa.postalcode_address
from device d
left join company c on c.company_id = d.company_id
left join (
  select company_id, postalcode_address,
    row_number() over (partition by company_id order by last_update desc) rn
  from postal_address
) pa on pa.company_id = c.company_id and pa.rn = 1;

Upvotes: 2

Related Questions