离线处理sql

里面用了一些自定义的日期函数,使用起来更加方便

当然hive也有一些日期函数 

from_unixtime(unix时间戳)   可以转换成日期时间,后面多参 可以加上格式

unix_timestamp("2019-10-10 11:11:11")  可以转换成unix时间戳   后面多参 可以加上格式

unix 时间戳是10位的 到秒     和13位的不一样  可以转换

cast(13位时间戳/1000  as bigint)                可以转换成unix时间戳

weekofyear("2019-10-10 11:11:11")   可以获取第几周

datediff(“2019-12-11”,"2020-11-11")  差值

to_date("2019-10-10 11:11:11")   2019-10-10 

 

简单的优化:

因为设置了分区表,所以可以进行先过滤一下。

 

 

CREATE external TABLE ext_startup_logs(
userId string,
appPlatform string,
appId string,
startTimeInMs bigint,
activeTimeInMs bigint,
appVersion string,
city string)
PARTITIONED BY (y string, m string,d string) 
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe' 
STORED AS TEXTFILE;

函数:
getdaybegin  getweekbegin  getmonthbegin  formattime

今天appid为app00001的不同版本的活跃用户数

select
appVersion,
count(distinct userId) c
from ext_startup_logs
where appId = "app00001"
and concat (y,m,d) = formattime(getdaybegin(),"yyyyMMdd")
group by appVersion

本周内每天各版本日活跃数
select
appVersion,
formattime(startTimeInMs,"yyyyMMdd") day,
count(distinct userId) c
from ext_startup_logs
where appId = 'app00001'
and concat (y,m,d) >= formattime(getweekbegin(),"yyyyMMdd")
group by appVersion,formattime(startTimeInMs,"yyyyMMdd")


select
formattime(startTimeInMs,'yyyyMMdd'),appVersion , count(distinct userId)
from ext_startup_logs 
where appId = 'app00001'
and concat(y,m,d) >= formattime(getweekbegin(),'yyyyMMdd')
group by formattime(startTimeInMs,'yyyyMMdd'), appVersion;


5.3.10 留存分析统计
1. 本周回流用户统计
本周回流用户:上周没有启动过,本周启动过





1. 任意日新增用户
		1)	今天新增用户
//选出用户的最小登陆时间大于今天的起始时间
select 
userId,
min(startTimeInMs)  minStartTime
from ext_startup_logs
group by userId
having 
minStartTime >= getdaybegin()
and  minStartTime < getdaybegin(1)    
		2)昨日新增用户
//最小的登陆时间大于昨天的起始时间,小于今天的起始时间
select 
userId,
min(startTimeInMs)  minStartTime
from ext_startup_logs
group by userId
having 
minStartTime >= getdaybegin(-1)
and  minStartTime < getdaybegin()  

		3)指定日期的 2019/1/1 10:10:10 
select 
userId,
min(startTimeInMs)  minStartTime
from ext_startup_logs
group by userId
having 
minStartTime >= getdaybegin("2019/1/1 10:10:10")
and  minStartTime < getdaybegin("2019/1/1 10:10:10",1)

2. 任意周新增用户
1)	本周新增用户
select 
userId,
min(startTimeInMs)  minStartTime
from ext_startup_logs
group by userId
having 
minStartTime >= getweekbegin()
and  minStartTime < getdaybegin(1)


活跃用户统计
1)	日活跃用户数
//启动记录有很多条,每天都有重复的
//求今天的活跃用户,
//求启动时间 >今天开始毫秒值 并 <明天开始毫秒值
//然后去重,count 一下
select 
count(distinct userId)
from ext_startup_logs
where startTimeInMs > getdaybegin() 
and startTimeInMs < getdaybegin(1)

//周活跃人数
select 
count(distinct userId)
from ext_startup_logs
where startTimeInMs > getweekbegin() 
and startTimeInMs < getweekbegin(1)

1)	一次查询出一周内,每天的日活跃数
//startTimeInMs 要大于本周的起始时间  小于下周的起始时间
//按日期分组     count(distinct userId)

select 
count(distinct userId),
formattime(startTimeInMs,"yyyy/MM/dd") day
from ext_startup_logs
where startTimeInMs >getweekbegin()
and startTimeInMs < getweekbegin(1)
group by formattime(startTimeInMs,"yyyy/MM/dd")
1553145326514
1489029488  
2)	一次查询出过去的5周,每周的周活跃数
from_unixtime(1323308943)  这种是unix的时间戳  用不了  只精确到毫秒
weekofyear('2016-12-08 10:03:01')

select 
count(distinct userId),
weekofyear(formattime(startTimeInMs,"yyyy-MM-dd HH:mm:ss")) week
from ext_startup_logs
where startTimeInMs >getweekbegin(-4)
and startTimeInMs < getweekbegin(1)
group by  weekofyear(formattime(startTimeInMs,"yyyy-MM-dd HH:mm:ss"))

//不用自定义的函数 
select 
count(distinct userId),
weekofyear(from_unixtime(cast (startTimeInMs/1000 as bigint) )  ) week
from ext_startup_logs
where startTimeInMs >getweekbegin(-4)
and startTimeInMs < getweekbegin(1)
group by  weekofyear(from_unixtime(cast (startTimeInMs/1000 as bigint)) )

3)	一次查询出过去的三个月内,每月的月活跃数

select 
count(distinct userId),
formattime(startTimeInMs,"yyyy-MM",0) month
from ext_startup_logs
where startTimeInMs >getmonthbegin(-3)
and startTimeInMs < getmonthbegin()
group by  formattime(startTimeInMs,"yyyy-MM",0) month


select 
count(distinct userId),
formattime(startTimeInMs,"yyyy-MM",0) month
from ext_startup_logs
where startTimeInMs >getmonthbegin(-3)
and startTimeInMs < getmonthbegin()
group by  formattime(startTimeInMs,"yyyy-MM",0) 


优化 ,根据分区表先进行过滤

//过去的五周(包含本周)某个app每周的周活跃用户数
select 
count(distinct userId),
weekofyear(formattime(startTimeInMs,"yyyy-MM-dd HH:mm:ss")) week
from ext_startup_logs
where concat(y,m,d) >= formattime(getweekbegin(-4),"yyyyMMdd")
and  concat(y,m,d) < formattime(getweekbegin(1),"yyyyMMdd")
group by  weekofyear(formattime(startTimeInMs,"yyyy-MM-dd HH:mm:ss"))


连续活跃3周用户统计

select
userId,
count(distinct weekofyear(from_unixtime(cast(startTimeInMs/1000 as bigint)))) c
from ext_startup_logs
where concat(y,m,d) >= formattime(getweekbegin(-2),"yyyyMMdd")
group by userId 
having c>=3

沉默用户统计
//只启动了一次   之后不再启动  对应只有一条日志
//刚安装了两天的不算沉默用户
select
userId,
count(userId) c
from ext_startup_logs
where concat(y,m,d) < formattime(getdaybegin(-1),"yyyyMMdd")
group by userId 
having c = 1 


今天启动次数统计











 

  屏蔽数据库间的差异,以统一的界面和操作方式来对数据进行处理,将程序员或数据管理员从繁琐的操作模式中解脱出来,使其更加专注的进行Sql语句的编写。   将查询分析器和企业管理器功能合二为一,并融入众多实用功能,并完美支持 sqlserver、oracle、mysql、access。是您编写sql语句和数据分析的绝佳帮手。   功能列表:   1、代码高亮:根据操作数据库的不同区分相应关键字,并高亮显示   2、自动完成:输入不同的表名等信息,将自动提示相关的字段信息等。   3、智能提示:输入相应关键字将出现类似VS中一样的说明提示。   4、跨库操作:可同时跨多个数据库间操作,互不影响。随时切换,随时运行。   5、随意运行:运行选择的代码、运行多个Sql操作代码。如果运行多个Select语句则显示多个结果集,用来对比查看。(快捷键F5)   6、数据库树中查找对象。   7、在Sql语句编辑器中快速查找功能。   8、查看数据库属*   9、查看表结构   10、自动生成Sql语句模板   11、删除表、视图等   12、查看数据库属*,并快速定到至物理文件。   13、生成脚本信息功能   14、结果集导出功能。   15、在结构集中查找   16、删除指定的行(快捷键 ‘delete’)   17、修改制定的数据   18、添加新数据   19、复制选择内容(快捷键‘ctrl+C’)   20、将外部数据导入到结果集中(支持txt:以 '|'或tab符号为分割符 和execl: 指定Sheet页名称 和 默认Sheet页 )   21、支持将导入的数据更新至数据库。   22、编辑操作时自动错提示功能(如:自动递增字段自动屏蔽编辑功能。必填字段没有填写内   容则提示,数据类型不正确自动提示等。。。)   23、自动标识主键(主键字段标识为-*-字段名-*-)   24、冻结指定行、列功能。使查看操作更加方便   25、数据集更改后,在提交前可选查看所有更改的部分数据。并用颜**分。   26、自定义我的收藏功能。   27、详细数据单窗体查看功能(支持图片字段)   28、*在没有安装SqlServer 客户端及任何组件的情况下仍然可以连接至SqlServer*   29、*判断Oracle的常见错误,并尝试更改或给出提示*   30、增加历史记录功能。   31、增加自动保存用户状态功能。再次打开软件将会保留上次已连接的数据库信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值