简体   繁体   中英

FInd the match data in SQL

I have a table called Product. It has column called colors. It contains data as follows:

在此处输入图像描述

The data type for colors is varchar . The database contains large product values (almost 2 hundred), and same applies to colors. Now I need to write an sql query script to find top 2 products having largest variation in colors.

You need to split and count the colors for each product(brand):

SELECT products, colors
FROM (
   SELECT 
      *,
      DENSE_RANK() OVER (ORDER BY (
         SELECT COUNT(DISTINCT LTRIM(RTRIM([value]))) 
         FROM STRING_SPLIT(SUBSTRING(colors, 2, LEN(colors) - 2), ',')) DESC
      ) AS rank
   -- FROM YourTable
   FROM (VALUES
      ('adidas', '{red, blue, black}'),
      ('puma', '{red, green, blue, orange}'),
      ('nike', '{red, green}')
   ) product (products, colors)
) t
WHERE rank <= 2

Result:

products colors 
----------------------------------
puma     {red, green, blue, orange}
adidas   {red, blue, black}

If you need to count the colors, simply add one additional column:

SELECT *
FROM (
   SELECT 
      *,
      DENSE_RANK() OVER (ORDER BY (
         SELECT COUNT(DISTINCT RTRIM(LTRIM([value]))) 
         FROM STRING_SPLIT(SUBSTRING(colors, 2, LEN(colors) - 2), ',')) DESC
      ) AS rank,
      (
         SELECT COUNT(DISTINCT LTRIM(RTRIM([value]))) 
         FROM STRING_SPLIT(SUBSTRING(colors, 2, LEN(colors) - 2), ',')
      ) AS [count]
   FROM (VALUES
      ('adidas', '{red, blue, black, blue, red}'),
      ('puma', '{red, green, blue}'),
      ('nike', '{red, green}')
   ) product (products, colors)
) t
WHERE [count] > 2
WITH CTE(PRODUCT,COLOR) AS
(
  SELECT 'ADIDAS','{RED,BLUE}' UNION ALL
  SELECT 'REEBOK','{PURPLE,GRAY,BLACK,ORANGE}' UNION ALL
  SELECT 'NIKE','{}'UNION ALL
  SELECT 'PUMA','{RED,BLACK,ORANGE}'
)
SELECT TOP 2 C.PRODUCT,C.COLOR,LEN(C.COLOR)XX
FROM CTE AS C
ORDER BY XX DESC

Could you please try if the above is suitable for you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM