点击关注上方“SQL数据库开发”,
设为“置顶或星标”,第一时间送达干货
简单——会考察一些group by & limit之类的用法,或者平时用的不多的函数比如rand()类;会涉及到一些表之间的关联
中等——会考察一些窗口函数的基本用法;会有表之间的关联,相对tricky的地方在于会有一些自关联的使用
困难——会有中位数或者更加复杂的取数概念,可能要求按照某特定要求生成列;一般这种题建中间表会解得清晰些
order订单表,字段为:goods_id, amount ;
pv 浏览表,字段为:goods_id,uid;
goods按照总销售金额排序,分成top10,top10~top20,其他三组
create table if not exists test.nil_goods_category as select goods_id,case when nn<= 10 then 'top10'when nn<= 20 then 'top10~top20'else 'other' end as goods_groupfrom(select goods_id,row_number() over(partition by goods_id order by sale_sum desc) as nnfrom (select goods_id,sum(amount) as sale_sumfrom order group by 1 ) aa) bb;select b.goods_group,count(distinct a.uid) as numfrom pv aleft join test.nil_goods_category bon a.goods_id = b.goods_idgroup by 1;
(提示:可以左右滑动代码)
1.select count(distinct g_id) as event_goods_numfrom goods_eventwhere (t1<=t4 and t1>=t3)or (t2>=t3 and t2<=t4)2.select count(distinct g_id) as event_goods_numfrom goods_eventwhere (t1<=t4 and t1>=t3)union all
select a.goods_id,a.timefrom event ainner join(select goods_id,count(*)from eventgroup by gooods_idorder by count(*) desclimit 1) bon a.goods_id = b.goods_idorder by a.goods_id,a.time desc
drop table if exists koo.nil_temp0222_a2;create table if not exists koo.nil_temp0222_a2 asselect * ,row_number() over(partition by userid order by inserttime) as nn1from (select a.* ,b.inserttime as inserttime_aftr ,datediff(b.inserttime,a.inserttime) as session_difffrom (select userid,inserttime ,row_number() over(partition by userid order by inserttime asc) nnfrom koo.nil_temp0222where userid = 1900000169 ) aleft join (select userid,inserttime ,row_number() over(partition by userid order by inserttime asc) nnfrom koo.nil_temp0222where userid = 1900000169 ) bon a.userid = b.userid and a.nn = b.nn-1) aawhere session_diff >10 or nn = 1order by userid,inserttime;drop table if exists koo.nil_temp0222_a2_1;create table if not exists koo.nil_temp0222_a2_1 asselect a.*,case when b.nn is null then a.nn+3 else b.nn end as nn_endfrom koo.nil_temp0222_a2 aleft join koo.nil_temp0222_a2 bon a.userid = b.useridand a.nn1 = b.nn1 - 1;select a.*,b.nn1 as session_idfrom(select userid,inserttime ,row_number() over(partition by userid order by inserttime asc) nnfrom koo.nil_temp0222where userid = 1900000169) aleft join koo.nil_temp0222_a2_1 bon a.userid = b.useridand a.nn>=b.nnand a.nn<b.nn_end
select *from (select * ,rank() over(partition by mm order by dd desc) as nn1 ,row_number() over(partition by mm,dd order by inserttime desc) as nn2from (select cast(right(to_date(inserttime),2) as int) as dd,month(inserttime) as mm,userid,inserttimefrom koo.nil_temp0222) aa) bbwhere nn1 = 1 and nn2<=3;
select a.*,b.num as num2,c.num as num3from table aleft join table bon a.userid = b.useridand a.dt = date_add(b.dt,-1)left join table con a.userid = c.useridand a.dt = date_add(c.dt,-2)where b.num>100and a.num>100and c.num>100
每个特征列都完全匹配的情况下
最多有一个特征列不匹配,其他19个特征列都完全匹配,但哪个列不匹配未知
1.select aa.*from (select *,concat(d1,d2,d3……d20) as mmdfrom table) aaleft join (select id,concat(d1,d2,d3……d20) as mmdfrom table) bbon aa.id = bb.idand aa.mmd = bb.mmd2.select a.*,sum(d1_jp,d2_jp……,d20_jp) as same_judgefrom (select a.* ,case when a.d1 = b.d1 then 1 else 0 end as d1_jp ,case when a.d2 = b.d2 then 1 else 0 end as d2_jp ,case when a.d3 = b.d3 then 1 else 0 end as d3_jp ,case when a.d4 = b.d4 then 1 else 0 end as d4_jp ,case when a.d5 = b.d5 then 1 else 0 end as d5_jp ,case when a.d6 = b.d6 then 1 else 0 end as d6_jp ,case when a.d7 = b.d7 then 1 else 0 end as d7_jp ,case when a.d8 = b.d8 then 1 else 0 end as d8_jp ,case when a.d9 = b.d9 then 1 else 0 end as d9_jp ,case when a.d10 = b.d10 then 1 else 0 end as d10_jp ,case when a.d20 = b.d20 then 1 else 0 end as d20_jp ,case when a.d11 = b.d11 then 1 else 0 end as d11_jp ,case when a.d12 = b.d12 then 1 else 0 end as d12_jp ,case when a.d13 = b.d13 then 1 else 0 end as d13_jp ,case when a.d14 = b.d14 then 1 else 0 end as d14_jp ,case when a.d15 = b.d15 then 1 else 0 end as d15_jp ,case when a.d16 = b.d16 then 1 else 0 end as d16_jp ,case when a.d17 = b.d17 then 1 else 0 end as d17_jp ,case when a.d18 = b.d18 then 1 else 0 end as d18_jp ,case when a.d19 = b.d19 then 1 else 0 end as d19_jpfrom table aleft join table bon a.id = b.id) aawhere sum(d1_jp,d2_jp……,d20_jp) = 19
t的字段有:uid,goods_id,star。uid是用户id
goodsid是商品id
star是用户对该商品的评分,值为1-5
select aa.uid1,aa.uid2,sum(star_multi) as resultfrom (select a.uid as uid1 ,b.uid as uid2 ,a.goods_id ,a.star * b.star as star_multifrom t aleft join t bon a.goods_id = b.goods_idand a.udi<>b.uid) aagroup by 1,2
select uid1,uid2,sum(multiply) as resultfrom(select t.uid as uid1, t.uid as uid2, goods_id,a.star*star as multiplyfrom a left join bon a.goods_id = goods_idand a.uid<>uid) aagroup by goods
select a.*,b.s_mid_n,c.l_mid_n,avg(b.s_mid_n,c.l_mid_n)from (select case when mod(count(*),2) = 0 then count(*)/2 else (count(*)+1)/2 end as s_mid ,case when mod(count(*),2) = 0 then count(*)/2+1 else (count(*)+1)/2 end as l_midfrom table ) aleft join (select id,num,row_number() over(partition by id order by num asc) nnfrom table) bon a.s_mid = b.nnleft join (select id,num,row_number() over(partition by id order by num asc) nnfrom table) con a.l_mid = c.nn
select distinct credit_levelfrom (select credit_level,count(distinct nn) as numberfrom (select userid,credit_level,inserttime,month(inserttime) as mm ,weekofyear(inserttime) as week ,dense_rank() over(partition by credit_level,month(inserttime) order by weekofyear(inserttime) asc) as nnfrom koo.nil_temp0222where substring(inserttime,1,7) = '2019-12'order by credit_level ,inserttime ) aagroup by 1 ) bbwhere number = (select count(distinct weekofyear(inserttime))from koo.nil_temp0222where substring(inserttime,1,7) = '2019-12')
作者:adorable_new
来源:简书
本文为转载分享,如侵权请联系后台删除
——End——
后台回复关键字:1024,获取一份精心整理的技术干货 后台回复关键字:进群,带你进入高手如云的交流群。 推荐阅读
这是一个能学到技术的公众号,欢迎关注
点击「阅读原文」了解SQL训练营文章转载自SQL数据库开发,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




