508,465
社区成员




本次竞赛共有3个题目,将提供数据文件和参考的SQL解法,参赛选手要写出返回同样结果的SPL脚本。
可以假定所有数据能在内存中放下,代码中不必考虑外存计算。
最后提交SPL脚本文件(.splx)。
对于2、3项,如果两份答案的评分相同,则最早收到的答案获胜(以邮件发送时间为准)。
找出2021年3月18日(该日期可作为参数)上涨且已经连续上涨天数达到或超过4天(该天数可作为参数)的股票和相应的连续上涨天数。
参考SQL(使用MySQL8版本及以上,以下同)
with recursive
t1 as (select *, row_number() over(partition by code order by tdate) rn
from stock),
t2 as (select *
from t1 where tdate='2021-03-18'
union all
select t1.*
from t2 join t1 on t2.code=t1.code and t2.rn=t1.rn+1
where t2.price>t1.price)
select code,count(1)-1 cnt
from t2
group by code
having cnt>=4;
参考SPL解法
A | |
1 |
2021-03-18 |
2 |
=T("data.btx").select(tdate<=A1).sort(code,-tdate) |
3 |
=A2.group@o(code).select(tdate==A1) |
4 |
=A3.new(code,~.pselect(price<=price[1])-1:cnt) |
5 |
=A4.select(cnt>=4) |
参考SQL:
with
#dayofweek('1970-01-04')=1, 周日
t1 as (select *, datediff(tdate,'1970-01-04') div 7 as w,
price/lag(price) over(partition by code order by tdate) rise
from stock),
t2 as (select t1.code, t1.w, count(1) z0, sum(if(t1.rise>1,1,0)) z1,
sum(if(dayofweek(t1.tdate)=2 and t1.rise>1.01,1,0)) z2,
sum(if(dayofweek(t1.tdate)<>2 and t1.rise>1.03,1,0)) z3
from t1 join t1 e1 on t1.code=e1.code and t1.w=e1.w+1
join t1 e2 on t1.code=e2.code and t1.w=e2.w+1
where dayofweek(e1.tdate)=5 and e1.rise>1
and dayofweek(e2.tdate)=6 and e2.rise>1
group by code, w
having z0=5 and z1>=4 and z2=1 and z3>=1),
t4 as (select code, w, max(if(dayofweek(tdate)=6,price,null)) price5, max(price) h
from t1 group by code, w),
t5 as (select code, w, price5, lead(h) over(partition by code order by w) h1,
lead(h,2) over(partition by code order by w) h2
from t4),
t6 as (select count(1) n
from t5 join t2 using (code,w)
where t5.h1/t5.price5>1.1 or t5.h2/t5.price5>1.1)
select n/(select count(1) from t2) p from t6;
参考SQL:
with
q as (select *, row_number() over(partition by code order by tdate) n,
price/lag(price) over(partition by code order by tdate) rise
from stock),
q0 as (select *
from q where tdate between '2021-01-01' and '2021-03-31'),
q1 as (select code, n, 20-1 rg #range是mysql关键字
from q0 where rise>1.05),
q2 as (select q0.code, q0.n, q1.rg+q1.n-q0.n rg
from q0 join q1 on q0.code=q1.code and q0.n between q1.n+1 and q1.n+q1.rg
where q0.rise<0.95),
q3 as (select q0.code, q0.n, q2.rg+q2.n-q0.n rg
from q0 join q2 on q0.code=q2.code and q0.n between q2.n+1 and q2.n+q2.rg
where q0.rise>1.05),
q4 as (select q0.code, q0.n, q3.rg+q3.n-q0.n rg
from q0 join q3 on q0.code=q3.code and q0.n between q3.n+1 and q3.n+q3.rg
where q0.rise<0.95),
q5 as (select q0.code, q0.n, q4.rg+q4.n-q0.n rg
from q0 join q4 on q0.code=q4.code and q0.n between q4.n+1 and q4.n+q4.rg
where q0.rise>1.05)
select (select count(distinct code) from q0) n0,
(select count(distinct code) from q1) n1,
(select count(distinct code) from q2) n2,
(select count(distinct code) from q3) n3,
(select count(distinct code) from q4) n4,
(select count(distinct code) from q5) n5;
参考SQL:
with
q as (select *, price/lag(price) over(partition by code order by tdate) rise
from stock),
q1 as (select tdate, first_value(code) over(w1) code1, first_value(price) over(w1) price1
from q
where tdate between '2021-01-01' and '2021-03-31' and rise>1.095
window w1 as (partition by tdate order by price)),
q2 as (select * from q1 group by tdate),
q3 as (select tdate, first_value(code) over(w2) code2, first_value(price) over(w2) price2
from q
where tdate between '2021-01-01' and '2021-03-31' and rise<0.905
window w2 as (partition by tdate order by price desc)),
q4 as (select * from q3 group by tdate)
select q2.tdate, q2.code1, q2.price1, q4.code2, q4.price2
from q2 join q4 using (tdate)
where q2.price1<q4.price2
order by tdate;
1、扫码添加上方 哪吒 微信,咨询与本次竞赛相关的任何问题;
2、可通过 哪吒 微信申请加入技术交流群,沟通交流学习与SPL有关的技术问题。
来了