--sql如下,这是我计算出正确结果后,重新整理思路精简后的结果select yhdj as user_grade,round(count(*)/(selectcount(distinct uid) from tb_user_log),2) as ratio
from
(select uid,
casewhencount(*)>1andmin(dif)<7then'忠实用户'whencount(*)=1andmin(dif)<7then'新晋用户'whenmin(dif)>=7andmin(dif)<30then'沉睡用户'whenmin(dif)>=30then'流失用户'endas yhdj
from
(select uid,datediff(today,intime) as dif
from
(select uid,date(in_time) as intime,(selectmax(in_time) from tb_user_log) as today
from tb_user_log) a)b
groupby uid)c
groupby yhdj
orderby ratio desc
--这里面用到了之前做题时的知识点,一个是将日志表根据进出时间一分为二然后用union连接去重,得到一个登录表--另一个是聚合函数count()或者sum()带条件的用法,count(列名='**' or null),sum(if 列名='**'1,1,0)--一般count用null,sum用0--以下是sql实现selecttime,count(*) as dau,round(count(rannk=1ornull)/count(*),2) as lv
from
(select uid,time,rank() over(partitionby uid orderbytimeASC) as rannk
from
(select uid,date(in_time) astimefrom tb_user_log
unionselect uid,date(out_time) astimefrom tb_user_log)a
orderbytime)b
groupbytimeorderbytime--默认升序ASC,DESC为降序
---待注释select uid,timem month,sum(jbs) as coin
from
(select uid,DATE_FORMAT(time,'%Y%m') as timem,
casewhen rt=3then3when rt=0then7else1endas jbs
from
(select uid,time,rank() over(partitionby uid,raak orderbytime)%7as rt
from
(select uid,time,diff,sum(diff) over(partitionby uid orderbytime) as raak
from
(select uid,time,casewhen diff<2then0else1endas diff
from
(select uid,time,casewhen datediff(time,nextday) isnullthen0else datediff(time,nextday) endas diff
from
(select uid,time,lag(time,1,null) over(partitionby uid orderbytime) as nextday
from
(select uid,date(in_time) astimefrom tb_user_log
where sign_in=1and artical_id=0)a)b)c)d)e)f)g
groupby uid,timem
Copy
select a.order_id as'订单号',date(event_time) as'下单时间',
total_amount as'订单总额',
total_cnt as'订单数',
status,
b.product_id as'产品号',
price as'单价',
cnt as'数量',
shop_id as'店铺号',
in_price as'进价',
quantity as'进货数量',
release_time as'上架时间'from tb_order_overall a
inner join tb_order_detail b on
a.order_id=b.order_id
inner join tb_product_info c on
b.product_id=c.product_id
wheredate(event_time)>='2021-10-01'and shop_id='901'
Copy
SELECT
name
FROM
syscolumns
WHERE
ID = OBJECT_ID( '表名1' )
AND name IN ( SELECT name FROM syscolumns WHERE ID = OBJECT_ID( '表名2' ) );
该语句可以查询表一中都有哪些字段存在于表二中
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)