0

I have a simple table Customers with fields id, name, rating, seller_id. The result of query

select seller_id, MAX(rating) 
from Customer 
group by seller_id

is the customers grouped by seller_id with max values of the rating. Like this:

seller_id  rating
2          17
3          20
4          17

How to modify this query to get customer's id additionally?
If I add id to SELECT I get an error, that I should add id to grouping. But I want only get above shown values with id specified. Does anyone know how to do this? Thanks.

user3523923
  • 43
  • 1
  • 5

4 Answers4

0

Try this

select c1.* from Customer c1 inner join 
(select seller_id, MAX(rating) as rating from Customer 
group by seller_id) c2 on c1.rating  = c2.rating

or

 SELECT c1.* FROM Customer b1 LEFT JOIN Customer c2
 ON (b1.seller_id = b2.seller_id  AND b1.rating > b2.br_created_date)
 WHERE b2.rating is null;
0

Try:

select t.id, t.seller_id, t.rating
from Customer c
join (
    select seller_id, MAX(rating) rating
    from Customer 
    group by seller_id
) x
on x.seller_id = c.seller_id
and x.rating = c.rating
cybertextron
  • 10,547
  • 28
  • 104
  • 208
Praveen
  • 8,945
  • 4
  • 31
  • 49
0

SimarjeetSingh has a good answer. Just tweak a little more:

select c.customer_id, c.seller_id
from customer c
join (
  select seller_id, max(rating) max_rating
  from customer
  group by seller_id
) s 
  on c.seller_id = s.seller_id
  and c.rating = s.max_rating

This way you are comparing the seller_id and the maximum rating.

zedfoxus
  • 35,121
  • 5
  • 64
  • 63
0

A dataset is divided into groups using the GROUP BY clause. The grouping attribute is common key shared by members of each group. The grouping attribute is usually a single column but may be multiple columns or an expression that cannot be based on group functions. Note that only grouping attributes and group functions are permitted in the SELECT clause when using GROUP BY.

refer this Select EMP with max SAL from each DEPT

Community
  • 1
  • 1
venkat
  • 449
  • 4
  • 17