HIVE-SQL基础面试题(很多基础写不出来)

题1:编写SQL拉取存在连续4天的用户
数据源表:active_info
字段:userid(用户id),ftime(活跃日期)
字段样例:

user_id ftime
123 2022-07-10
234 2022-07-12

SOL返回字段:userid

答案

select distinct userid
from 
(	-- 添加排序序号
	select userid
		,row_number over(partition by userid order by ftime asc) as rnk 
		,ftime
	from (select userid,ftime from active_info group by userid,ftime) t -- 去重
) t1 
group by user_id,date_sub(ftime,rnk)
having count(1) >= 4 

题2:如下几段SQL分别返回什么
数仓,数分相关工作经验,最近在面试招聘,3-8年工作经验,这三条返回全写对,10人不超过2人;
表:ta

id
1
2
4
null

表:tb

id
2
2
4
3
  • 语句1:
select ta.id, tb.id as id2 from ta left join tb on ta.id = tb.id and tb.id > 2

答案

id id2
4 4
1 null
2 null
null null
  • 语句2:
select ta.id, tb.id as id2 from ta left join tb on ta.id = tb.id and ta.id < 3

答案

id id2
2 2
2 2
1 null
4 null
null null
  • 语句3:
select ta.id,tb.id as id2 from ta full join tb on ta.id = tb.id where ta.id is null

答案

id id2
null 3
null null

题3:编写SQL拉收6月专业公司为A、B、C的(只要这三个公司)
比较简单的一个行转列问题
小R用户数(月付费:[0.100)),
中R用户数 (月付费:[100,200)),
大R用户数 (月付费:[200,+∞))

表名:pay_info
字段
专业公司:business_cd
付费日期:statis_date(样式:2022-07-01)
付费金额:Pay
用户id:user_id

SQL返回字段(4个字段):
business_cd专公司,
large_pay_uv大R用户数,
mid_pay_uv中R付费用户数,
small_pay_uv小R付费用户数

答案

select business_cd
	,count(case when pay < 100 then 1 end) as small_pay_uv  -- 小R付费用户
	,count(case when pay >= 100 and pay < 200 then 1 end) as mid_pay_uv
	,count(case when pay >= 200 then 1 end) as large_pay_uv
from 
(	-- 先按专业公司,用户id对付费求和加总
	select business_cd
		,user_id
		,sum(pay) as pay
	from pay_info
	where month(statis_date)= '6' and business_cd in ('A','B','C')
	group by business_cd,user_id
) t 
group by business_cd

你可能感兴趣的:(SQL,sql,hive,数据库)