0

其中一个蜂巢表看起来像这样:

 ID    listOfcategories
    1     ["a","b","b","a","c","d","d"]
    2     ["a","a","a","c","c","c","c","e","e","e"]
    3     ["a","b","c"]

逗号分隔值的数量是一个变量。我想查询每行/ID 中不同类别的数量。 所以,我的输出应该是这样的:

ID     numDistCategories
1      4
2      3
3      3
4

1 回答 1

0

您可以使用explode每个类别输出单独的行,然后count distinct得到您正在寻找的结果。

像这样的东西。

SELECT 
    id, 
    COUNT(DISTINCT(cat)) as numDistCategories
FROM (
    SELECT 
        id, 
        EXPLODE(listOfcategories) AS cat 
    FROM myTable) a
GROUP BY id;

希望有帮助。

于 2013-08-22T14:23:57.693 回答