大数据开发面试题

1、一个股票的表里有如下字段id,code,time,price价格,请编写一个SQL统计改股票的所有波峰和波谷。(面试官口述的)

当时面试的时候我没有思路,在回来的路上突然想到一个思路也不知道是否正确。做了一个实验如下

hive> CREATE TABLE IF NOT EXISTS  stock(
       id int ,
       code bigint ,
       time string ,
       price double )
     ROW FORMAT DELIMITED
       FIELDS TERMINATED BY '\t'
     STORED AS textfile

       ;

 

112017-12-14 12:03:0612
2212017-12-14 12:03:0711 
3312017-12-14 12:03:0813
4412017-12-14 12:03:0910 
5512017-12-14 12:03:1015
6622017-12-14 12:03:1118
7722017-12-14 12:03:1216
8822017-12-14 12:03:1319

 

 

select a.id,a.price,
 case when b.price is null then "未知"
      when c.price is null then "未知"
      when a.price>b.price and a.price>c.price then "波峰"
      when a.price<b.price and a.price<c.price then "波谷"
 else "未知" end as mark
 from default.stock a 
 left join 
 default.stock b on a.code=b.code and b.id=a.id-1
 left join

 

 

default.stock c on a.code=c.code and c.id=a.id+1

 

 

或者

select code,
       time,price,
       case when g_price is null or d_price is null 
            then 'unknown'
            when price<g_price and price<d_price
            then 'lower'
            when price>g_price and price>d_price
            then 'hight'
            ELSE NULL END as p
  from (select code,time,price,lag(price,1)over(order by time asc) as g_price,lead(price,1)over(order by time asc) as d_price
         from stock

        ) a

 

 

2、表数据如何变成

 

实现如下结果

        code p1              

1 12,11 ,13,10 ,15    
         2 18,16,19            

select code,concat_ws(',',collect_set(price)) p1 from stock group by code

3.如何把上面的结果

 

        code p1              

1 12,11 ,13,10 ,15    

         2 18,16,19         变回如下结果

 

select code,add_new from  (select code,concat_ws(',',collect_set(price)) p1 from stock group by code) a lateral view explode(split(a.p1,',')) ad as add_new

 

 

大数据运维群584912368,欢迎运维同学加入

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值