Skip to main content
added 2 characters in body
Source Link
esqew
  • 44.3k
  • 27
  • 95
  • 134

 

I have two tables in Snowflake - one with color data and one with ranks for each color:

Color table: | Index | color1 | color2 | color3 | color4 | color5 | | -----| ----- | ----- | ----- | ----- |----- | | 1| red | orange |red |blue |violet | | 2| indigo | yellow | blue | yellow |violet |

Indexcolor1color2color3color4color5
1redorangeredblueviolet
2indigoyellowblueyellowviolet

Rank table: | Color | Rank | | ---- | ---- | | Red | 1 | | Orange | 2 | | Yellow | 3 | | Green | 4 | | Blue | 5 | | Indigo | 6 | | Orange | 7 | | Violet | 8 |

ColorRank
Red1
Orange2
Yellow3
Green4
Blue5
Indigo6
Orange7
Violet8

I'd like to use the two tables above to rank each colors in each row and show top 3 colors in the resulting table. So resulting table will have red, red, and orange for index 1 and yellow, yellow, and blue for index 2.

Index Rank1 Rank2 Rank3
1 red red orange
2 yellow yellow blue

Any help would be appreciated. Thanks!


 

I have two tables in Snowflake - one with color data and one with ranks for each color:

Color table: | Index | color1 | color2 | color3 | color4 | color5 | | -----| ----- | ----- | ----- | ----- |----- | | 1| red | orange |red |blue |violet | | 2| indigo | yellow | blue | yellow |violet |

Rank table: | Color | Rank | | ---- | ---- | | Red | 1 | | Orange | 2 | | Yellow | 3 | | Green | 4 | | Blue | 5 | | Indigo | 6 | | Orange | 7 | | Violet | 8 |

I'd like to use the two tables above to rank each colors in each row and show top 3 colors in the resulting table. So resulting table will have red, red, and orange for index 1 and yellow, yellow, and blue for index 2.

Index Rank1 Rank2 Rank3
1 red red orange
2 yellow yellow blue

Any help would be appreciated. Thanks!

I have two tables in Snowflake - one with color data and one with ranks for each color:

Color table:

Indexcolor1color2color3color4color5
1redorangeredblueviolet
2indigoyellowblueyellowviolet

Rank table:

ColorRank
Red1
Orange2
Yellow3
Green4
Blue5
Indigo6
Orange7
Violet8

I'd like to use the two tables above to rank each colors in each row and show top 3 colors in the resulting table. So resulting table will have red, red, and orange for index 1 and yellow, yellow, and blue for index 2.

Index Rank1 Rank2 Rank3
1 red red orange
2 yellow yellow blue

Any help would be appreciated. Thanks!

Source Link
CLV
  • 21
  • 2

SQL rank and reorder columns


I have two tables in Snowflake - one with color data and one with ranks for each color:

Color table: | Index | color1 | color2 | color3 | color4 | color5 | | -----| ----- | ----- | ----- | ----- |----- | | 1| red | orange |red |blue |violet | | 2| indigo | yellow | blue | yellow |violet |

Rank table: | Color | Rank | | ---- | ---- | | Red | 1 | | Orange | 2 | | Yellow | 3 | | Green | 4 | | Blue | 5 | | Indigo | 6 | | Orange | 7 | | Violet | 8 |

I'd like to use the two tables above to rank each colors in each row and show top 3 colors in the resulting table. So resulting table will have red, red, and orange for index 1 and yellow, yellow, and blue for index 2.

Index Rank1 Rank2 Rank3
1 red red orange
2 yellow yellow blue

Any help would be appreciated. Thanks!