You can use aggregate functions:
select title ScoreBand, count(*) TotalNoPeople, avg(p.score) AvgScore from scoreperson p inner join scoreminmax m on p.score between m.minscore and m.maxscore group by Title order by cast(left(title, 2) as int)
If you donβt have a person in the existing range, you can give us something like this:
select case when title is not null then title else 'No Range' end ScoreBand, count(personid) TotalNoPeople, avg(p.score) AvgScore from scoreperson p left join scoreminmax m on p.score between m.minscore and m.maxscore group by id, Title order by id
edit # 2, based on your comments you can use:
select m.title ScoreBand, count(p.personid) TotalNoPeople, avg(p.score) AvgScore from scoreminmax m left join scoreperson p on p.score between m.minscore and m.maxscore group by m.id, m.Title order by m.id;
Taryn source share