As other commentators note, you donβt show exactly how you connect people and colors. If you use a reference table (person_id, color_id), then there is no way to solve this problem in standard SQL, because it requires summary or cross-tabulation, which is not part of standard SQL.
If you want to add the condition that the number of colors is limited and known, and the development time, you can come up with a solution using one connection for each color, as well as the CASE or IF functions in SQL. But this would not be elegant, and, in addition, I would not trust this state in order to remain faithful for a very long time.
If you can come up with a different way of storing information about color binding, you may have more options to create the result you want, but another method of storage involves some degree of database denormalization, which can lead to other difficulties.
Otherwise, you will need to do this in the stored procedure or application code.
Larry lustig
source share