1

I have some codes like this. I want to count the number of phone number (isdn) by partition (month) and volume of data use (g.volumn)

select partition,
    case when a.g_volume = 0 then '0MB'
         when a.g_volume <=10 and a.g_volume > 0 then '0-10MB'
         when a.g_volume <=50 and a.g_volume >10 then '10 - 50MB'
    end as data,
    count(distinct a.isdn) as num_isdn
from 
    (select partition, g_volume, sub_type, infras, num_register_day, isdn
    from f121_tot_charge_accum_final
    where partition in ('2020101','2020102','2020103','2020104')) a
group by partition, 
    case when a.g_volume = 0 then '0MB'
         when a.g_volume <=10 and a.g_volume > 0 then '0-10MB'
         when a.g_volume <=50 and a.g_volume >10 then '10 - 50MB'
    end as data;

But SQL says error like this

**Query execution failed
Reason:
SQL Error: org.apache.spark.sql.catalyst.parser.ParseException: 
mismatched input 'as' expecting {<EOF>, ',', '.', '[', 'GROUPING', 'ORDER', 'HAVING', 'LIMIT', 'OR', 'AND', 'IN', NOT, 'BETWEEN', 'LIKE', RLIKE, 'IS', 'WINDOW', 'WITH', 'UNION', 'EXCEPT', 'INTERSECT', EQ, '<=>', '<>', '!=', '<', LTE, '>', GTE, '+', '-', '*', '/', '%', 'DIV', '&', '|', '^', 'SORT', 'CLUSTER', 'DISTRIBUTE'}(line 15, pos 5)**

Can someone help me please. I don't understand why.

4

1 回答 1

1

您的 group by 具有列别名定义:

case when a.g_volume = 0 then '0MB'
     when a.g_volume <=10 and a.g_volume > 0 then '0-10MB'
     when a.g_volume <=50 and a.g_volume >10 then '10 - 50MB'
end as data;
----^

您需要删除它。列别名只能在select

group by partition, 
    case when a.g_volume = 0 then '0MB'
         when a.g_volume <=10 and a.g_volume > 0 then '0-10MB'
         when a.g_volume <=50 and a.g_volume >10 then '10 - 50MB'
    end ;

也就是说,我认为 Hive 可以允许以下位置group by

group by 1, 2;
于 2020-06-26T10:37:32.130 回答