Всего тремя цветами - при условии, что все значения уникальны - вы можете получить с помощью case
выражений:
select ((case when red > blue and red > yellow then 'red'
when blue > yellow then 'blue'
else 'yellow'
end) + '/' +
(case when red > blue and red < yellow or red < blue and red > yellow then 'red'
when blue > red and blue < yellow or blue < red and blue < yellow then 'blue'
else 'yellow'
end)
)
Результаты немного отличаются от заявленных вами результатов.Доминирующий цвет - первый, но это похоже на выгоду.
Если вы хотите использовать cross apply
, то один (относительно) простой метод:
select max(c.color) + '/' + min(c.color)
from t cross apply
(select top (2) v.*
from (values ('red', red), ('blue', blue), ('yellow', yellow)) v(color, val)
order by val desc
) c;
Цветане упорядочены по их стоимости.Вы можете легко организовать это:
select max(case when seqnum = 1 then c.color en) + '/' + max(case when seqnum = 1 then c.color end)
from t cross apply
(select v.*, row_number() over (order by v.val desc) as seqnum
from (values ('red', red), ('blue', blue), ('yellow', yellow)) v(color, val)
) c;