I have been trying to solve this for a while now, any help would be greatly appreciated.
I have tables:
distributor(distributor_name, distributor_id)
distributed(price, movie_id, distributor_id)
movie(title, movie_id)
Each distributor has multiple records for movies at different prices.
I am trying to select the minimum price for a particular movie and from what distributor
so far I have tried:
select min(Price), Distributor from
(select m.distributor_name as Distributor, d.price from
distributors m, distributed d, movies mo
where
mo.title = 'Movie Name' and mo.movie_id = d.movie_id and d.distributor_id = m.distributor_id)
group by Distributor;
Which returns the minimum price for each distributor.
If I run:
select min(Price)
(select m.distributor_name as Distributor, d.price from
distributors m, distributed d, movies mo
where
mo.title = 'Movie Name' and mo.movie_id = d.movie_id and d.distributor_id = m.distributor_id);
It gives just the lowest price of that particular movie.
How can I get the name of the distributor for the lowest price?
Depending on your version of Oracle, you can use
row_number():See SQL Fiddle with Demo
If you wanted to expand on this and get the lowest price for any movie, then you could partition the data by movie title: