SQL 虽然可以对集合中的记录进行循环计算, 但在循环计算过程中利用中间变量、同时计算多个值、前后记录访问、减少循环次数等方面差强人意。而集算器 SPL 则要直观许多,可以按自然思维习惯写出运算。这里对 SQL 和集算器 SPL 在循环计算方面进行了对比。细节参考:SQL 难点解决:循环计算
MySQL8:
with recursive t(n) as (
select 1
union all select n+1 from t where n<20)
select n from t
where n!=1 and n not in (select t1.n*t2.n from t t1 join t t2 on t1.n<=t2.n where t1.n>1 and t2.n between 2 and 20/2 and t1.n*t2.n<=20);
SPL写法:
+查看SPL语句
MySQL8:
with recursive t1 as (select *,row_number() over(order by tdate) rn from stktrade where sid='000651'),
t2 as (select *,0 rise from t1 where rn=1
union all
select t1.*, if(t1.close>t2.close,t2.rise+1,0) from t1 join t2 on t1.rn=t2.rn+1)
select max(rise) from t2;
SPL写法:
+查看SPL语句
MySQL8:
select 1-t2.close/t1.close fall
from (select max(close) close from stktrade where sid='300469') t1,
(select close from stktrade where sid='300469' and tdate='2018-02-14') t2;
SPL写法:
+查看SPL语句
MySQL8:
select tdate,volume,sum(volume) over(order by tdate) cum
from stktrade
where sid='300469' and tdate between '2018-01-01' and '2018-01-10';
SPL写法:
+查看SPL语句
MySQL8:
with t as (select row_number() over(order by tdate) rn,
sum(volume) over(order by tdate) cum
from stktrade
where sid='300469' and tdate>='2018-01-01')
select min(rn) from t where cum>=1000000;
SPL写法:
+查看SPL语句
MySQL8:
with tt(start,end) as (select date'2010-01-07',date'2010-01-9'
union all select date'2010-01-15',date'2010-01-16'
union all select date'2010-01-07',date'2010-01-12'
union all select date'2010-01-08',date'2010-01-11'),
t as (select * from tt order by start),
tmp as (
select t.start, t.end, @m:=if(@m>@p,@m,@p) m,@p:=end,
case when @m>end then 0 when @m
SPL写法:
+查看SPL语句
7、 列出信息发展 (300469) 和招商银行 (600036) 从 2018 年 6 月 11 日到 15 日的交易信息及累积换手率
MySQL8:
with k as (select sid,circulation,tdate start,lead(tdate,1, date_add(now(),interval 1 day))over(partition by sid order by tdate) end
from stocks)
select t.*, k.circulation circ, sum(t.volume/k.circulation/10000) over(partition by sid order by tdate) rate
from stktrade t join k on t.sid=k.sid and t.tdate>=k.start and t.tdate
SPL写法:
+查看SPL语句
8、 列出招商银行 (600036)2018 年 1 月 1 日到 10 日每天的 20 日收盘均价
MySQL8:
with t as (select *,row_number() over(order by tdate) rn from stktrade where sid='600036'),
t1 as (select * from t where tdate between '2018-01-01' and '2018-01-10')
select t1.tdate, t1.close, avg(t.close) ma20
from t1 join t on t.rn between t1.rn-19 and t1.rn
group by t1.tdate;
SPL写法:
+查看SPL语句
9、 列出官方语言最多的国家的名称、人口、元首及官方语言数
MySQL8:
select Name, Population, HeadOfState, top.Num
from world.country
join (
select countrycode, count(*) as num
from world.countrylanguage
where isofficial='T'
group by countrycode
having num = (
select max(summary.n)
from (
select countrycode, count(*) as n
from world.countrylanguage
where isofficial='T'
group by countrycode
) as summary
)
) as top on country.code=top.countrycode;
SPL写法:
+查看SPL语句
你可能感兴趣的:(MySQL,SPL,数据库,集算器,循环计算,SQL比较)