How to join many to many relationship on max value for relationship field

bqui56

I have three tables:

Shop_Table
   shop_id
   shop_name

Sells_Table
   shop_id
   item_id
   price

Item_Table
   item_id
   item_name

The Sells_Table links the item and shop tables via FK's to their ids. I am trying to get the most expensive item from each store, i.e., output of the form:

(shop_name, item_name, price)
(shop_name, item_name, price)
(shop_name, item_name, price)
(shop_name, item_name, price)
...

where price is the max price item for each shop. I can seem to achieve (shop_name, max(price)) but when I try to include the item_name I am getting multiple entries for the shop_name. My current method is

create view shop_sells_item as
select s.shop_name as shop, i.item_name as item, price
from Shop_Table s
join Sells_Table on (s.shop_id = Sells_Table.shop_id)
join Item_Table i on (i.item_id = Sells_Table.item_id);

select shop, item, max(price)
from shop_sells_item
group by shop;

However, I get an error saying that item must appear in the GROUP BY clause or be used in an aggregate function, but if I include it then I don't get the max price for each shop, instead I get the max price for each shop,item pair which is of no use.

Also, is using a view the best way? could it be done via a single query?

miazo

Please note that the query below doesn't deal with a situation where multiple items in one store have the same maximum price (they are all the most expensive ones):

SELECT
    s.shop_name,
    i.item_name,
    si.price
FROM
    Sells_Table si
JOIN
    Shop_Table s
ON
    si.shop_id = s.shop_id
JOIN
    Item_Table i
ON
    si.item_id = i.item_id
WHERE
    (shop_id, price) IN (
        SELECT
            shop_id,
            MAX(price) AS price_max
        FROM
            Sells_Table
        GROUP BY
            shop_id
    );

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to JOIN tables in many-to-many relationship

From Dev

Find max value from a many-to-many relationship in SQL

From Dev

Get attribute value from the join in a many-to-many relationship

From Dev

get extra field value in Django's many-to-many relationship

From Dev

how to flatten many to many relationship

From Dev

how to query a many to many relationship?

From Dev

How to save many to many relationship?

From Dev

how to flatten many to many relationship

From Dev

How can I filter a field from a Many To Many relationship in Rails?

From Dev

Sqlalchemy one to many relationship join?

From Dev

Many to one relationship, unwanted JOIN

From Dev

Exclusive join on has many relationship

From Dev

Rails field on join entity in has_many through relationship

From Dev

JPQL left outer join on many to many relationship

From Dev

Laravel Complicated inner join on many to many relationship

From Dev

Oracle Recursive Join - Many to Many Relationship

From Dev

Inner Join Between Many to Many Relationship Models

From Dev

Ebean many to many relationship with join fails

From Dev

Rails: many to many relationship join table design

From Dev

MySQL, join many to many relationship query

From Dev

Mongoose field one to many relationship

From Dev

How to create join table record in Rails, has many: through relationship

From Dev

How to join a one-to-many relationship in Entity Framework?

From Dev

Is this a many-to-many relationship?

From Dev

Many to Many relationship in Ecto

From Dev

Many to many relationship and MySQL

From Dev

implementing a many to many relationship

From Dev

Many to many relationship optimization

From Dev

many to many powerpivot relationship

Related Related

HotTag

Archive