常用SQL案例
Posted 不务正业RD从产品经理转行到数据分析
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了常用SQL案例相关的知识,希望对你有一定的参考价值。
一、开窗函数
1.开窗函数求占比
select
city_name,
type,
id_cnt_by_city_name,
count(distinct id) id_cnt, --type分组ID数
count(distinct id) / id_cnt_by_b as rate --占比
from
(
select
type,
city_name,
id,
count(id) over (partition by city_name) id_cnt_by_city_name --求城市总id数
from
table_Name
) xxx
group by
city_name,
type,
id_cnt_by_city_name
2.随机抽样
-- 根据type_1,每组抽样600
select
*
from
(
select
id,
type_1,
type_2,
row_number() over(partition by type_1 order by rand()) as rn --rand() 获取随机数, order by排序,row_number() 排名次序
from
table_name
)a
where rn <= 600 --抽样600
二、josn数据解析
学会Hive解析Json数组_000X000的博客-CSDN博客_hive解析json
以上是关于常用SQL案例的主要内容,如果未能解决你的问题,请参考以下文章