查找不同类别的产品

问题描述

我被要求找到产品的不同类别,但问题是表 category_code 具有“category.sub_category.product”格式的值。现在我的问题是,如何找到具有这种格式的值的不同类别?

解决方法

使用split函数将以点为分隔符的category_code拆分成数组(category,sub_category,product),然后获取category的element[0]:

select distinct split(category_code,'\\.')[0] as category
  from your table