Hello! I have a table of breweries that make various beers. I have grouped the number of beers brewed to each brewery, but now, I need to group by top 25%, then next 25%, etc.; grouping into 4 categories by percentage of beers brewed.
Here's what I have so far:
Select b.brewery_id, b.name as "Brewery Name", count(b.brewery_id) as "Number of Beers Brewed"
From DB2SLATE.beerdb_beers c
right Join DB2SLATE.beerdb_breweries b
on b.brewery_id = c.brewery_id
group by b.brewery_id, b.name order by "Number of Beers Brewed" desc;
Thank you! I have solved 90% of my problem. Now, I need to sort by brewery_id within each "Rank". Here's my query and sample data:
Select b.brewery_id, b.name as "Brewery Name",
count(b.brewery_id) as "Number of Beers Brewed",
ntile(4) over (order by count(b.brewery_id)desc) as Rank
From DB2SLATE.beerdb_beers c
right Join DB2SLATE.beerdb_breweries b
on b.brewery_id = c.brewery_id group by b.brewery_id, b.name;
Select b.brewery_id, b.name as "Brewery Name",
count(b.brewery_id) as "Number of Beers Brewed",
ntile(4) over (order by count(b.brewery_id)desc) as Rank
From DB2SLATE.beerdb_beers c
right Join DB2SLATE.beerdb_breweries b
on b.brewery_id = c.brewery_id group by b.brewery_id, b.name
ORDER BY [Rank],[Number of Beers Brewed] DESC,b.brewery_id