select t1.Name || t2.Name as CombinedCat, t1.Percent * t2.Percent as CombinedPc from your_table t1 join your_table t2 on t2.Category = 'Speed' where t1.Category = 'Color' order by CombinedCat
EDIT: setting in problem description
If you want to do this with a dynamic number of categories, you can do it with the following query that uses a recursive CTE:
with Categories as ( select category, row_number() over (order by category) as seq from your_table group by category), RecursiveCTE (Name, Percent, seq) as ( select t.Name, t.Percent, c.seq from your_table t join Categories c on c.category = t.category and c.seq = 1 union all select r.Name || t.Name as Name, r.Percent * t.Percent as Percent, c.seq from RecursiveCTE r join Categories c on c.seq = r.seq + 1 join your_table t on t.category = c.category ) select t.Name as CombinedCat, Percent as CombinedPc from RecursiveCTE t where t.seq = (select max(seq) from Categories) order by t.Name
SQLFiddle Demo
The above query combines the category names in alphabetical order, but you can customize this by changing the order by clause in the row_number() function in Categories CTE:
row_number() over (order by category) as seq
sstan
source share