Beyond Relational TSQL Challenge 44
Here is an interesting challenge which might give you a fun time categorizing products by color. The first part of your job is to identify the unique color combinations of products and the second part is to identify the products that have the given color combination.
ItemID Color
----------- ----------
1 Red
1 Green
2 Red
2 Green
2 Yellow
2 Orange
3 Red
4 Green
5 Blue
6 Orange
ColorCombination Occurrences Items
----------------------- ----------- ---------
Blue 1 5
Green 3 1,2,4
Orange 2 2,6
Red 3 1,2,3
Yellow 1 2
Green+Orange 1 2
Green+Red 2 1,2
Green+Yellow 1 2
Orange+Red 1 2
Orange+Yellow 1 2
Red+Yellow 1 2
Green+Orange+Red 1 2
Green+Orange+Yellow 1 2
Green+Red+Yellow 1 2
Orange+Red+Yellow 1 2
Green+Orange+Red+Yellow 1 2
- Rules
- The output should be ordered by the number of colors in the color combination column and then by the full color combination name. First all the single color names should come in alphabetical order. This should be followed by all the color combinations that contains two colors, and then three colors etc.
- The items column should display the values sorted in ascending order.
- A color will be associated with an item only once.
- Your solution should not assume any limits on the number of colors or the number of items other than what can be placed as a list in a varchar(max) column.
- For a single color combination the list of colors should be in ascending order, i.e. Green+Orange+Red not Green+Red+Orange.
- Color names will be alphanumeric only.
- Colors will appear case-insensitive, i.e. if you have 'Orange' you will not have 'ORANGE' or 'orange' or any other variant in the data.