IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> 蚂蚁金服大数据面试题-蚂蚁森林 -> 正文阅读

[大数据]蚂蚁金服大数据面试题-蚂蚁森林

----题目

一.蚂蚁森林植物申领统计

问题:假设2017年1月1日开始记录低碳数据(user_low_carbon),假设2017年10月1日之前满足申领条件的用户都申领了一颗p004-胡杨,剩余的能量全部用来领取“p002-沙柳” 。统计在10月1日累计申领“p002-沙柳” 排名前10的用户信息;以及他比后一名多领了几颗沙柳。

得到的统计结果如下表样式:

user_id plant_count less_count(比后一名多领了几颗沙柳)

u_101 1000 100

u_088 900 400

u_103 500 …

1.创建表

create table user_low_carbon(user_id String,data_dt String,low_carbon int) row format delimited fields terminated by '\t';
?
create table plant_carbon(plant_id string,plant_name String,low_carbon int) row format delimited fields terminated by '\t';

2.加载数据

load data local inpath "/opt/module/hive/datas/user_low_carbon.txt" into table user_low_carbon;
?
load data local inpath "/opt/module/hive/datas/plant_carbon.txt" into table plant_carbon;

3.--转化时间字段

select
 user_id,
 regexp_replace(data_dt,'/','-'),
 low_carbon 
from user_low_carbon
 where regexp_replace(data_dt,'/','-') <'2017-10-01' t1

4.-- 求能量总和

select
 t1.user_id,
 sum(t1.low_carbon)-(select low_carbon from plant_carbon where plant_name='胡杨') low_carbon_sum
from (
 ? ? select
 ? ?  user_id,
 ? ?  regexp_replace(data_dt,'/','-'),
 ? ?  low_carbon 
 ? ? from user_low_carbon
 ? ? ?where regexp_replace(data_dt,'/','-') <'2017-10-01'
)t1
group by t1.user_id ? t2

5.--求出比后一名多领了几棵

select
  t2.user_id,
  t2.low_carbon_sum
 ?from(
select
  t1.userId,
 ?sum(t1.low_carbon)-(select low_carbon from plant_carbon where plant_name='胡杨')
 ?from(
 ?select
  user_id,
  regexp_replace(data_dt,'/','-')
  low_carbon
 ?from user_id_carbon
 ?where regexp_rplace(data_dt,'/','-')<'2017-10-1' 
 ?) t1
 ?group by t1.user_id )
  t2
 )
 ?where t2.low_carbon_sum>0
 ?order by t2.low_carbon_sum desc
 ?limit 11
 ?) t3

6.答案

select 
 t4.user_id,
 t4.plant_count,
 t4.plant_count-lead(t4.plant_count,1,0) over(order by t4.plant_count desc) less_count
 from (
 ? ?select
 ? ? t3.user_id,
 ? ? floor(t3.low_carbon_sum/(select low_carbon from plant_carbon where plant_name='沙柳')) plant_count
 ? ?from(
 ? ? ? ? select
 ? ? ? ? t2.user_id,
 ? ? ? ? t2.low_carbon_sum
 ? ? ? ? from (
 ? ? ? ? ? ? ?select
 ? ? ? ? ? ? ? t1.user_id,
 ? ? ? ? ? ? ? sum(t1.low_carbon)-(select low_carbon from plant_carbon where plant_name='胡杨') low_carbon_sum
 ? ? ? ? ? ? ?from (
 ? ? ? ? ? ? ? ? ? select
 ? ? ? ? ? ? ? ? ?  user_id,
 ? ? ? ? ? ? ? ? ?  regexp_replace(data_dt,'/','-'),
 ? ? ? ? ? ? ? ? ?  low_carbon 
 ? ? ? ? ? ? ? ? ? from user_low_carbon
 ? ? ? ? ? ? ? ? ? ?where regexp_replace(data_dt,'/','-') <'2017-10-01'
 ? ? ? ? ? ? ?)t1
 ? ? ? ? ? ? ?group by t1.user_id
 ? ? ? ? )t2
 ? ? ? ? where t2.low_carbon_sum>0
 ? ? ? ? order by  t2.low_carbon_sum desc 
 ? ? ? ? limit 11
 ? ?) t3
)t4

结果:

| t4.user_id  | t4.plant_count  | less_count  |
+-------------+-----------------+-------------+
| u_007 ? ? ? | 66 ? ? ? ? ? ?  | 3 ? ? ? ? ? |
| u_013 ? ? ? | 63 ? ? ? ? ? ?  | 10 ? ? ? ?  |
| u_008 ? ? ? | 53 ? ? ? ? ? ?  | 7 ? ? ? ? ? |
| u_005 ? ? ? | 46 ? ? ? ? ? ?  | 1 ? ? ? ? ? |
| u_010 ? ? ? | 45 ? ? ? ? ? ?  | 1 ? ? ? ? ? |
| u_014 ? ? ? | 44 ? ? ? ? ? ?  | 5 ? ? ? ? ? |
| u_011 ? ? ? | 39 ? ? ? ? ? ?  | 2 ? ? ? ? ? |
| u_009 ? ? ? | 37 ? ? ? ? ? ?  | 5 ? ? ? ? ? |
| u_006 ? ? ? | 32 ? ? ? ? ? ?  | 9 ? ? ? ? ? |
| u_002 ? ? ? | 23 ? ? ? ? ? ?  | 1 ? ? ? ? ? |
| u_004 ? ? ? | 22 ? ? ? ? ? ?  | 22 ? ? ? ?  |
+-------------+-----------------+-------------+

二、蚂蚁森林低碳用户排名分析

问题:查询user_low_carbon表中每日流水记录,条件为:

用户在2017年,连续三天(或以上)的天数里,每天减少碳排放(low_carbon)都超过100g的用户低碳流水。

需要查询返回满足以上条件的user_low_carbon表中的记录流水。

例如用户u_002符合条件的记录如下,因为2017/1/2~2017/1/5连续四天的碳排放量之和都大于等于100g:

seq(key) user_id data_dt low_carbon

xxxxx10 u_002 2017/1/2 150

xxxxx11 u_002 2017/1/2 70

xxxxx12 u_002 2017/1/3 30

xxxxx13 u_002 2017/1/3 80

xxxxx14 u_002 2017/1/4 150

xxxxx14 u_002 2017/1/5 101

备注:统计方法不限于sql、procedure、python,java等

方法一:

--1.查找到在2017年的用户
select
 user_id,
 regexp_replace(data_dt,'/','-') data_dt,
 sum(low_carbon) low_carbon_sum 
from user_low_carbon
 where year(regexp_replace(data_dt,'/','-')) ='2017'
group by user_id,regexp_replace(data_dt,'/','-') t1
?
--2.筛选2017年的用户里碳排放超过100g的用户
select
 t1.user_id,
 t1.data_dt,
 t1.low_carbon_sum
from (
 ? ? ?select
 ? ? ? user_id,
 ? ? ? regexp_replace(data_dt,'/','-') data_dt,
 ? ? ? sum(low_carbon) low_carbon_sum 
 ? ? ?from user_low_carbon
 ? ? ? where year(regexp_replace(data_dt,'/','-')) ='2017'
 ? ? ?group by user_id,regexp_replace(data_dt,'/','-')
 ? ? ?)t1
where t1.low_carbon_sum>=100  t2
?
2021-2-1 ?2021-2-2 ?2021-2-3
2021-2-2 ?2021-2-3 ?2021-2-4
2021-2-3 ?2021-2-4 ?2021-2-5
?
--3.通过lag和lead开窗函数筛选第三天和第二天碳排放达标的用户,分为今天,昨天,前天,明天,后天
select
 t2.user_id,
 t2.low_carbon_sum,
 lag(t2.data_dt,2,'0000-00-00')over(partition by t2.user_id order by t2.data_dt) qt,
 lag(t2.data_dt,1,'0000-00-00')over(partition by t2.user_id order by t2.data_dt) zt,
 t2.data_dt,
 lead(t2.data_dt,1,'9999-99-99')over(partition by t2.user_id order by t2.data_dt) mt,
 lead(t2.data_dt,2,'9999-99-99')over(partition by t2.user_id order by t2.data_dt) ht
from (
 ? ? select
 ? ?  t1.user_id,
 ? ?  t1.data_dt,
 ? ?  t1.low_carbon_sum
 ? ? from (
 ? ? ? ? ? select
 ? ? ? ? ?  user_id,
 ? ? ? ? ?  regexp_replace(data_dt,'/','-') data_dt,
 ? ? ? ? ? ?sum(low_carbon) low_carbon_sum 
 ? ? ? ? ? from user_low_carbon
 ? ? ? ? ? ?where year(regexp_replace(data_dt,'/','-')) ='2017'
 ? ? ? ? ? group by user_id,regexp_replace(data_dt,'/','-')
 ? ? ? ? ? )t1
 ? ? where t1.low_carbon_sum>=100 
)t2 ? t3
?
--4.判断 天数是否正确,今天-前天,今天-昨天,今天-明天,今天-后天这四个的低碳能量是否大于100g
select
 t3.user_id,
 t3.data_dt,
 t3.low_carbon_sum,
 datediff(t3.data_dt,t3.qt) jt_qt,
 datediff(t3.data_dt,t3.zt) jt_zt,
 datediff(t3.data_dt,t3.mt) jt_mt,
 datediff(t3.data_dt,t3.ht) jt_ht
from (
 ? ? select
 ? ?  t2.user_id,
 ? ?  t2.low_carbon_sum,
 ? ?  lag(t2.data_dt,2,'0000-00-00')over(partition by t2.user_id order by t2.data_dt) qt,
 ? ?  lag(t2.data_dt,1,'0000-00-00')over(partition by t2.user_id order by t2.data_dt) zt,
 ? ?  t2.data_dt,
 ? ?  lead(t2.data_dt,1,'9999-99-99')over(partition by t2.user_id order by t2.data_dt) mt,
 ? ?  lead(t2.data_dt,2,'9999-99-99')over(partition by t2.user_id order by t2.data_dt) ht
 ? ? from (
 ? ? ? ? ?select
 ? ? ? ? ? t1.user_id,
 ? ? ? ? ? t1.data_dt,
 ? ? ? ? ? t1.low_carbon_sum
 ? ? ? ? ?from (
 ? ? ? ? ? ? ? ?select
 ? ? ? ? ? ? ? ? user_id,
 ? ? ? ? ? ? ? ? regexp_replace(data_dt,'/','-') data_dt,
 ? ? ? ? ? ? ? ? sum(low_carbon) low_carbon_sum 
 ? ? ? ? ? ? ? ?from user_low_carbon
 ? ? ? ? ? ? ? ? where year(regexp_replace(data_dt,'/','-')) ='2017'
 ? ? ? ? ? ? ? ?group by user_id,regexp_replace(data_dt,'/','-')
 ? ? ? ? ? ? ? ?)t1
 ? ? ? ? ?where t1.low_carbon_sum>=100 
 ? ? )t2 
)t3  t4
?
?
--5.查询减少碳排放大于等于两天的用户
select
 t4.user_id,
 t4.data_dt,
 t4.low_carbon_sum
from (
 ? ?select
 ? ? t3.user_id,
 ? ? t3.data_dt,
 ? ? t3.low_carbon_sum,
 ? ? datediff(t3.data_dt,t3.qt) jt_qt,
 ? ? datediff(t3.data_dt,t3.zt) jt_zt,
 ? ? datediff(t3.data_dt,t3.mt) jt_mt,
 ? ? datediff(t3.data_dt,t3.ht) jt_ht
 ? ?from (
 ? ? ? ? select
 ? ? ? ?  t2.user_id,
 ? ? ? ?  t2.low_carbon_sum,
 ? ? ? ?  lag(t2.data_dt,2,'0000-00-00')over(partition by t2.user_id order by t2.data_dt) qt,
 ? ? ? ?  lag(t2.data_dt,1,'0000-00-00')over(partition by t2.user_id order by t2.data_dt) zt,
 ? ? ? ?  t2.data_dt,
 ? ? ? ?  lead(t2.data_dt,1,'9999-99-99')over(partition by t2.user_id order by t2.data_dt) mt,
 ? ? ? ?  lead(t2.data_dt,2,'9999-99-99')over(partition by t2.user_id order by t2.data_dt) ht
 ? ? ? ? from (
 ? ? ? ? ? ? ?select
 ? ? ? ? ? ? ? t1.user_id,
 ? ? ? ? ? ? ? t1.data_dt,
 ? ? ? ? ? ? ? t1.low_carbon_sum
 ? ? ? ? ? ? ?from (
 ? ? ? ? ? ? ? ? ? ?select
 ? ? ? ? ? ? ? ? ? ? user_id,
 ? ? ? ? ? ? ? ? ? ? regexp_replace(data_dt,'/','-') data_dt,
 ? ? ? ? ? ? ? ? ? ? sum(low_carbon) low_carbon_sum 
 ? ? ? ? ? ? ? ? ? ?from user_low_carbon
 ? ? ? ? ? ? ? ? ? ? where year(regexp_replace(data_dt,'/','-')) ='2017'
 ? ? ? ? ? ? ? ? ? ?group by user_id,regexp_replace(data_dt,'/','-')
 ? ? ? ? ? ? ? ? ? ?)t1
 ? ? ? ? ? ? ?where t1.low_carbon_sum>=100 
 ? ? ? ? )t2 
 ? ?)t3 
 ? ?)t4
where (t4.jt_qt =2 and t4.jt_zt=1)or(t4.jt_zt =1 and t4.jt_mt=-1)or(t4.jt_mt =-1 and t4.jt_ht=-2) t5
?
--6.与(user_low_carbon)相连接
select
 t6.user_id,
 t6.data_dt,
 t6.low_carbon
from(
 ? ?select
 ? ? t4.user_id,
 ? ? t4.data_dt,
 ? ? t4.low_carbon_sum
 ? ?from (
 ? ? ? ?select
 ? ? ? ? t3.user_id,
 ? ? ? ? t3.data_dt,
 ? ? ? ? t3.low_carbon_sum,
 ? ? ? ? datediff(t3.data_dt,t3.qt) jt_qt,
 ? ? ? ? datediff(t3.data_dt,t3.zt) jt_zt,
 ? ? ? ? datediff(t3.data_dt,t3.mt) jt_mt,
 ? ? ? ? datediff(t3.data_dt,t3.ht) jt_ht
 ? ? ? ?from (
 ? ? ? ? ? ? select
 ? ? ? ? ? ?  t2.user_id,
 ? ? ? ? ? ?  t2.low_carbon_sum,
 ? ? ? ? ? ?  lag(t2.data_dt,2,'0000-00-00')over(partition by t2.user_id order by t2.data_dt) qt,
 ? ? ? ? ? ?  lag(t2.data_dt,1,'0000-00-00')over(partition by t2.user_id order by t2.data_dt) zt,
 ? ? ? ? ? ?  t2.data_dt,
 ? ? ? ? ? ?  lead(t2.data_dt,1,'9999-99-99')over(partition by t2.user_id order by t2.data_dt) mt,
 ? ? ? ? ? ?  lead(t2.data_dt,2,'9999-99-99')over(partition by t2.user_id order by t2.data_dt) ht
 ? ? ? ? ? ? from (
 ? ? ? ? ? ? ? ? ?select
 ? ? ? ? ? ? ? ? ? t1.user_id,
 ? ? ? ? ? ? ? ? ? t1.data_dt,
 ? ? ? ? ? ? ? ? ? t1.low_carbon_sum
 ? ? ? ? ? ? ? ? ?from (
 ? ? ? ? ? ? ? ? ? ? ? ?select
 ? ? ? ? ? ? ? ? ? ? ? ? user_id,
 ? ? ? ? ? ? ? ? ? ? ? ? regexp_replace(data_dt,'/','-') data_dt,
 ? ? ? ? ? ? ? ? ? ? ? ? sum(low_carbon) low_carbon_sum 
 ? ? ? ? ? ? ? ? ? ? ? ?from user_low_carbon
 ? ? ? ? ? ? ? ? ? ? ? ? where year(regexp_replace(data_dt,'/','-')) ='2017'
 ? ? ? ? ? ? ? ? ? ? ? ?group by user_id,regexp_replace(data_dt,'/','-')
 ? ? ? ? ? ? ? ? ? ? ? ?)t1
 ? ? ? ? ? ? ? ? ?where t1.low_carbon_sum>=100 
 ? ? ? ? ? ? )t2 
 ? ? ? ?)t3 
 ? ? ? ?)t4
 ? ?where (t4.jt_qt =2 and t4.jt_zt=1)or(t4.jt_zt =1 and t4.jt_mt=-1)or(t4.jt_mt =-1 and t4.jt_ht=-2)
) t5 join (
 ? ? ? ? ? select
 ? ? ? ? ?  user_id,
 ? ? ? ? ?  regexp_replace(data_dt,'/','-') data_dt,
 ? ? ? ? ?  low_carbon
 ? ? ? ? ? from user_low_carbon
 ? ? ? ? ? ?where year(regexp_replace(data_dt,'/','-')) ='2017'
)t6
on t5.user_id=t6.user_id and t5.data_dt=t6.data_dt

结果:

+-------------+-------------+----------------+
| t6.user_id  | t6.data_dt  | t6.low_carbon  |
+-------------+-------------+----------------+
| u_002 ? ? ? | 2017-1-2 ?  | 150 ? ? ? ? ?  |
| u_002 ? ? ? | 2017-1-2 ?  | 70 ? ? ? ? ? ? |
| u_002 ? ? ? | 2017-1-3 ?  | 30 ? ? ? ? ? ? |
| u_002 ? ? ? | 2017-1-3 ?  | 80 ? ? ? ? ? ? |
| u_002 ? ? ? | 2017-1-4 ?  | 150 ? ? ? ? ?  |
| u_002 ? ? ? | 2017-1-5 ?  | 101 ? ? ? ? ?  |
| u_005 ? ? ? | 2017-1-2 ?  | 50 ? ? ? ? ? ? |
| u_005 ? ? ? | 2017-1-2 ?  | 80 ? ? ? ? ? ? |
| u_005 ? ? ? | 2017-1-3 ?  | 180 ? ? ? ? ?  |
| u_005 ? ? ? | 2017-1-4 ?  | 180 ? ? ? ? ?  |
| u_005 ? ? ? | 2017-1-4 ?  | 10 ? ? ? ? ? ? |
| u_008 ? ? ? | 2017-1-4 ?  | 260 ? ? ? ? ?  |
| u_008 ? ? ? | 2017-1-5 ?  | 360 ? ? ? ? ?  |
| u_008 ? ? ? | 2017-1-6 ?  | 160 ? ? ? ? ?  |
| u_008 ? ? ? | 2017-1-7 ?  | 60 ? ? ? ? ? ? |
| u_008 ? ? ? | 2017-1-7 ?  | 60 ? ? ? ? ? ? |
| u_009 ? ? ? | 2017-1-2 ?  | 70 ? ? ? ? ? ? |
| u_009 ? ? ? | 2017-1-2 ?  | 70 ? ? ? ? ? ? |
| u_009 ? ? ? | 2017-1-3 ?  | 170 ? ? ? ? ?  |
| u_009 ? ? ? | 2017-1-4 ?  | 270 ? ? ? ? ?  |
| u_010 ? ? ? | 2017-1-4 ?  | 90 ? ? ? ? ? ? |
| u_010 ? ? ? | 2017-1-4 ?  | 80 ? ? ? ? ? ? |
| u_010 ? ? ? | 2017-1-5 ?  | 90 ? ? ? ? ? ? |
| u_010 ? ? ? | 2017-1-5 ?  | 90 ? ? ? ? ? ? |
| u_010 ? ? ? | 2017-1-6 ?  | 190 ? ? ? ? ?  |
| u_010 ? ? ? | 2017-1-7 ?  | 90 ? ? ? ? ? ? |
| u_010 ? ? ? | 2017-1-7 ?  | 90 ? ? ? ? ? ? |
| u_011 ? ? ? | 2017-1-1 ?  | 110 ? ? ? ? ?  |
| u_011 ? ? ? | 2017-1-2 ?  | 100 ? ? ? ? ?  |
| u_011 ? ? ? | 2017-1-2 ?  | 100 ? ? ? ? ?  |
| u_011 ? ? ? | 2017-1-3 ?  | 120 ? ? ? ? ?  |
| u_011 ? ? ? | 2017-1-4 ?  | 100 ? ? ? ? ?  |
| u_011 ? ? ? | 2017-1-5 ?  | 100 ? ? ? ? ?  |
| u_011 ? ? ? | 2017-1-6 ?  | 100 ? ? ? ? ?  |
| u_011 ? ? ? | 2017-1-7 ?  | 130 ? ? ? ? ?  |
| u_011 ? ? ? | 2017-1-7 ?  | 100 ? ? ? ? ?  |
| u_013 ? ? ? | 2017-1-2 ?  | 150 ? ? ? ? ?  |
| u_013 ? ? ? | 2017-1-2 ?  | 50 ? ? ? ? ? ? |
| u_013 ? ? ? | 2017-1-3 ?  | 150 ? ? ? ? ?  |
| u_013 ? ? ? | 2017-1-4 ?  | 550 ? ? ? ? ?  |
| u_013 ? ? ? | 2017-1-5 ?  | 350 ? ? ? ? ?  |
| u_014 ? ? ? | 2017-1-5 ?  | 250 ? ? ? ? ?  |
| u_014 ? ? ? | 2017-1-6 ?  | 120 ? ? ? ? ?  |
| u_014 ? ? ? | 2017-1-7 ?  | 270 ? ? ? ? ?  |
| u_014 ? ? ? | 2017-1-7 ?  | 20 ? ? ? ? ? ? |
+-------------+-------------+----------------+

方法二:

select
 t1.user_id,
 t1.data_dt,
 t1.low_carbon_sum,
 rank()over(partition by t1.user_id order by t1.data_dt) rk
from (
 ? ? ?select
 ? ? ? user_id,
 ? ? ? regexp_replace(data_dt,'/','-') data_dt,
 ? ? ? sum(low_carbon) low_carbon_sum 
 ? ? ?from user_low_carbon
 ? ? ? where year(regexp_replace(data_dt,'/','-')) ='2017'
 ? ? ?group by user_id,regexp_replace(data_dt,'/','-')
 ? ? ?)t1
where t1.low_carbon_sum>=100  t2
?
--等差数列less_dt
select
 t2.user_id,
 t2.data_dt,
 t2.low_carbon_sum,
 date_sub(t2.data_dt,t2.rk) less_dt
from (
 ? ? select
 ? ?  t1.user_id,
 ? ?  t1.data_dt,
 ? ?  t1.low_carbon_sum,
 ? ?  rank()over(partition by t1.user_id order by t1.data_dt) rk
 ? ? from (
 ? ? ? ? ? select
 ? ? ? ? ?  user_id,
 ? ? ? ? ?  regexp_replace(data_dt,'/','-') data_dt,
 ? ? ? ? ? ?sum(low_carbon) low_carbon_sum 
 ? ? ? ? ? from user_low_carbon
 ? ? ? ? ? ?where year(regexp_replace(data_dt,'/','-')) ='2017'
 ? ? ? ? ? group by user_id,regexp_replace(data_dt,'/','-')
 ? ? ? ? ? )t1
 ? ? where t1.low_carbon_sum>=100
)t2  t3
?
?
select
 t3.user_id,
 t3.data_dt,
 t3.low_carbon_sum,
 count(*)over(partition by t3.user_id,t3.less_dt) c_dt
from (
 ? ?select
 ? ? t2.user_id,
 ? ? t2.data_dt,
 ? ? t2.low_carbon_sum,
 ? ? date_sub(t2.data_dt,t2.rk) less_dt
 ? ?from (
 ? ? ? ? select
 ? ? ? ?  t1.user_id,
 ? ? ? ?  t1.data_dt,
 ? ? ? ?  t1.low_carbon_sum,
 ? ? ? ?  rank()over(partition by t1.user_id order by t1.data_dt) rk
 ? ? ? ? from (
 ? ? ? ? ? ? ? select
 ? ? ? ? ? ? ?  user_id,
 ? ? ? ? ? ? ?  regexp_replace(data_dt,'/','-') data_dt,
 ? ? ? ? ? ? ? ?sum(low_carbon) low_carbon_sum 
 ? ? ? ? ? ? ? from user_low_carbon
 ? ? ? ? ? ? ? ?where year(regexp_replace(data_dt,'/','-')) ='2017'
 ? ? ? ? ? ? ? group by user_id,regexp_replace(data_dt,'/','-')
 ? ? ? ? ? ? ? )t1
 ? ? ? ? where t1.low_carbon_sum>=100
 ? ?)t2
)t3  t4
?
?
?
select
t4.user_id,
t4.data_dt,
t4.low_carbon_sum
from (
 ? ?select
 ? ? t3.user_id,
 ? ? t3.data_dt,
 ? ? t3.low_carbon_sum,
 ? ? count(*)over(partition by t3.user_id,t3.less_dt) c_dt
 ? ?from (
 ? ? ? ?select
 ? ? ? ? t2.user_id,
 ? ? ? ? t2.data_dt,
 ? ? ? ? t2.low_carbon_sum,
 ? ? ? ? date_sub(t2.data_dt,t2.rk) less_dt
 ? ? ? ?from (
 ? ? ? ? ? ? select
 ? ? ? ? ? ?  t1.user_id,
 ? ? ? ? ? ?  t1.data_dt,
 ? ? ? ? ? ?  t1.low_carbon_sum,
 ? ? ? ? ? ?  rank()over(partition by t1.user_id order by t1.data_dt) rk
 ? ? ? ? ? ? from (
 ? ? ? ? ? ? ? ? ? select
 ? ? ? ? ? ? ? ? ?  user_id,
 ? ? ? ? ? ? ? ? ?  regexp_replace(data_dt,'/','-') data_dt,
 ? ? ? ? ? ? ? ? ? ?sum(low_carbon) low_carbon_sum 
 ? ? ? ? ? ? ? ? ? from user_low_carbon
 ? ? ? ? ? ? ? ? ? ?where year(regexp_replace(data_dt,'/','-')) ='2017'
 ? ? ? ? ? ? ? ? ? group by user_id,regexp_replace(data_dt,'/','-')
 ? ? ? ? ? ? ? ? ? )t1
 ? ? ? ? ? ? where t1.low_carbon_sum>=100
 ? ? ? ?)t2
 ? ?)t3
)t4
where c_dt>=3

提供的数据说明:

user_low_carbon:

u_001 2017/1/1 10

u_001 2017/1/2 150

u_001 2017/1/2 110

u_001 2017/1/2 10

u_001 2017/1/4 50

u_001 2017/1/4 10

u_001 2017/1/6 45

u_001 2017/1/6 90

u_002 2017/1/1 10

u_002 2017/1/2 150

u_002 2017/1/2 70

u_002 2017/1/3 30

u_002 2017/1/3 80

u_002 2017/1/4 150

u_002 2017/1/5 101

u_002 2017/1/6 68

...

plant_carbon:

p001 梭梭树 17

p002 沙柳 19

p003 樟子树 146

p004 胡杨 215

  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2021-08-02 10:53:03  更:2021-08-02 10:53:38 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/22 5:43:52-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码