SQL 难点解决:循环计算

SQL 虽然可以对集合中的记录进行循环计算, 但在循环计算过程中利用中间变量、同时计算多个值、前后记录访问、减少循环次数等方面差强人意。而集算器 SPL 则要直观许多,可以按自然思维习惯写出运算。这里对 SQL 和集算器 SPL 在循环计算方面进行了对比。细节参考:SQL 难点解决:循环计算

1、 求 20 以内的质数

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语句


2、 求格力电器 (000651) 最长上涨天数

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语句


3、 求信息发展 (300469)2018 年 2 月 14 日距历史最高收盘价跌幅

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语句


4、 求信息发展 (300469)2018 年 1 月 1 日到 10 日每天累积成交量

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语句


5、 从 2018 年 1 月 1 日起信息发展 (300469) 经过多少交易日总成交量达到 1000000 股

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语句


6、 重叠部分不重复计数时求多个时间段包含的总天数

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<start then datediff(end,start)+1 else datediff(end,@m) end days

from t,(select @m:=date'1970-01-01',@p:=@m) s

)

select sum(days) from tmp;

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<k.end

where t.sid in ('300469','600036') and t.tdate between '2018-06-11' and '2018-06-15';

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语句

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值