10.條件函數:case when
--如:select case when age<20 then "20歲如下"
when age>=20 and age<30 then "20~30歲"
when age>=30 and age<40 then "30~40歲"
else "40歲以上" end as age_type,
count (distinct user_id) user_num
from user_info
group by ...;
11.if函數
if(條件表達式,結果1,結果2) :當條件爲真-->結果1,不然結果2
--如:select if (level>5,"高","低") [as level_type] from...
12.對json字符串和map類型的處理
get_json_object(string json_string,string path)
string json_string:列名
string path:用$.key取值
--如:字段: extra1(string): {"systemtype":"ios","education":"master","marriage_status":"1","phone brand":"iphone X"}
--字段: extra2(map<string,string>): {"systemtype":"ios","education":"master","marriage_status":"1","phone brand":"iphone X"}
對於json類型:
例如:
SELECT
get_json_object(extra1, '$.phonebrand') as phone_brand,
count(distinct user_id) user_num
FROM user_info
GROUP BY get_json_object(extra1, '$.phonebrand');
對於map類型:
例如:select
extra2['phonebrand'] as phone_brand,
count(distinct user_id) user_num
FROM user_info
GROUP BY extra2['phonebrand'];