add batch remove batch split batch comment selection show hidden batches hide batch highlight batch
db<>fiddle
donate feedback about
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
create table orders(consumer_id int,cuisine_id int);
insert into orders values
(1,100)
,(1,101)
,(2,101)
,(3,101)
,(3,101)
,(1,101)
,(4,100)
,(4,101)
,(4,102)
,(4,103)
,(5,100)
,(5,101)
,(5,102)
,(5,103)
;

14 rows affected
WITH CuisineDiversity AS (
SELECT consumer_id,
COUNT(DISTINCT cuisine_id) AS distinct_cuisines_tried,
row_number() OVER (ORDER BY COUNT(DISTINCT cuisine_id) DESC,consumer_id) AS diversity_rank
FROM orders
GROUP BY consumer_id
)
SELECT consumer_id,
distinct_cuisines_tried,
diversity_rank
FROM CuisineDiversity
ORDER BY
distinct_cuisines_tried DESC,
diversity_rank ASC,
consumer_id ASC;
consumer_id distinct_cuisines_tried diversity_rank
4 4 1
5 4 2
1 2 3
2 1 4
3 1 5