sql 时间 练习

浏览: 1737
SQL

sql 时间

SELECT NOW() 显示当前时间

SELECT paidTime,date(paidTime),date_format(paidTime,'%Y-%m')from data.orderinfo 改变日期的显示格式

SELECT date_add(paidTime,INTERVAL 1 day)from data.orderinfo 在原来的日期上加一天,

datediff()求差值

统计不同月份下单人数

SELECT  month(paidTime),count(DISTINCT `userId`) from data.orderinfo

where `isPaid` = '已支付'

group by month(paidTime)

统计用户三月份的回购率和复购率

复购率

select count(ct) ,count(if(ct>1,1,NULL))from (

SELECT  userId,count(userId) as ct from data.orderinfo

where `isPaid` = '已支付'and  month(paidTime) = 3

group by userId ) t

回购率

select t1.ct,count(t1.ct),count(t2.ct),count(t2.ct)/count(t1.ct) from (

SELECT userId,date_format(paidTime,'%Y-%m-01') as ct from data.orderinfo

where `isPaid` = '已支付'

group by userId,date_format(paidTime,'%Y-%m-01'))as t1

left join (

SELECT userId,date_format(paidTime,'%Y-%m-01') as ct from data.orderinfo

where `isPaid` = '已支付'

group by userId,date_format(paidTime,'%Y-%m-01')) as t2

on t1.userId=t2.userId and t1.ct =date_sub(t2.ct,interval 1 month)

group by t1.ct

统计男女用户的消费频率是否由差异

select sex,avg(ct) from (

SELECT orderinfo.userId ,sex,COUNT(1) as ct FROM data.orderinfo 

inner join (

select * from data.`userinfo`

where sex <> '') t

on orderinfo.userId=t.userId

group by userId,sex) t2

group by sex

多次消费的用户,最后一次和第一次的消费间隔是多少

select userId,max(paidTime),min(paidTime),max(paidTime)-min(paidTime),datediff(max(paidTime),min(paidTime)) from data.orderinfo

where isPaid = '已支付'

group by userId having count(1)>1

统计不同年龄段,用户的消费金额是否由差异

select age,avg(ct) from(

select o.userId,age,count(o.userId) as ct from data.orderinfo o

inner join (

select userId,ceil((year(now())-year(birth))/10) as age from data.userinfo

where birth>'1901-00-00') t

where  isPaid ='已支付'

on o.userId=t.userId

where  isPaid ='已支付'

GROUP BY o.userId,age) t2

group by age

统计消费的二八法则,消费top20%的用户,贡献了多少额度

推荐 0
本文由 寒江 创作,采用 知识共享署名-相同方式共享 3.0 中国大陆许可协议 进行许可。
转载、引用前需联系作者,并署名作者且注明文章出处。
本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责。本站是一个个人学习交流的平台,并不用于任何商业目的,如果有任何问题,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

0 个评论

要回复文章请先登录注册