1

If it have a table as below: DB Table

I want to get each shops top salesperson, their total sales and the stores manager. If i query and get the max total sales and group by shop title, how do I also get the correct salesperson?

Ben
  • 345
  • 2
  • 10

2 Answers2

1

You can use a correlated subquery:

select t.*
from t
where t.total_sales = (select max(t2.total_sales)
                       from t t2
                       where t2.shop_title = t.shop_title
                      );
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
1

You could use inner join on subquery for max sales

select m.salesperson, t.max_sales, t.shop_title
from my_table m 
inner join (
    select max(sales) max_sales, shop_title
    from my_table
    group by shop_title
) t on t.shop_title = m.shop_title and t.max_sale = m.sales
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107