最具影响力的数字化技术在线社区

168大数据

 找回密码
 立即注册

QQ登录

只需一步,快速开始

1 2 3 4 5
打印 上一主题 下一主题
开启左侧

SQL 难点解决:序列生成

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-21 11:25:33 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多数据大咖,获取更多知识干货,轻松玩转大数据

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
1、    生成连续整数序列
MySQL8:  with recursive t(n) as (
select 1
union all
select n+1 from t where n<7
)
select * from t;

Oracle:select level n
from dual connect by level<=7;

集算器 SPL:

A
1
=to(1,7)
A1:构造从 1 到 7 的整数序列
示例 1:百鸡问题,鸡翁一值钱五,鸡母一值钱三,鸡雏三值钱一。百钱买百鸡,问鸡翁、母、雏各几
MySQL8:  
with recursive jg(n) as (select 1 union all select n+1 from jg where n<100/5),
jm(n) as (select 1 union all select n+1 from jm where n<100/3),
jc(n) as (select 3 union all select n+3 from jc where n<98)
select jg.n jw, jm.n jm, jc.n jc
from jg cross join jm cross join jc
where jg.n*5+jm.n*3+jc.n/3=100 and jg.n+jm.n+jc.n=100
      
集算器 SPL:

A
1
=to(100/5)
2
=to(100/3)
3
=33.(~*3)
4
=create(jw,jm,jc)
5
>A1.run(A2.run(A3.run(if(A1.~+A2.~+A3.~==100   && A1.~*5+A2.~*3+A3.~/3==100,A4.insert(0,A1.~,A2.~,A3.~)))))
A1:构造1到20的整数序列
A2:构造1到33的整数序列
A3:构造1到99且步长为3的整数序列
A4:创建数据结构为(jw,jm,jc)的序表
A5:对A1、A2、A3的数据进行嵌套循环,若满足于A1成员+A2成员+A3成员==100且A1成员*5+A2成员*3+A3成员/3==100则追加到A4序表中
示例2:将指定列中冒号分隔的串划分成多行
Oracle:
with t(k,f) as (select 1 , 'a1:a2:a3' from dual
union all select 2, 'b1:b2' from dual),
t1 as (select k,f, length(f)-length(replace(f,':',''))+1 cnt from t),
t2 as (select level n from dual connect by level<=(select max(cnt) from t1)),
t3 as (select t1.k, t1.f, n, cnt,
case when n=1 then 1 else instr(f,':',1,n-1)+1 end p1,  
case when n=cnt then length(f)+1 else instr(f,':',1,n) end p2
from t1 join t2 on t2.n<=t1.cnt)
select k,substr(f,p1,p2-p1) f from t3 order by k;
集算器 SPL:

A
1
=create(k,f).record([1,"a1:a2:a3",2,"b1:b2"])
2
>A1.run(f=f.split(":"))
3
=A1.(f.new(A1.k:k,   ~:f))
4
=A3.conj()
A1:创建数据结构为(k,f)的序表,并追加2条记录(1, “a1:a2:a3)和(2,”b1:b2”)
A2:将A1的字段f用冒号划分成序列并重新赋值给字段f
A3:针对A1每条记录构造数据结构为(k,f)的序表,并根据字段f中成员构造记录(A1.k,f成员)追加到此序表中
2、    生成连续日期序列
MySQL8:
with recursive
t(d) as (select date'2018-10-03'
union all
select d+1 from t where d<date'2018-10-09')
select d,dayofweek(d) w from t;

集算器 SPL:

A
1
=periods("2018-10-03",   "2018-10-09")
A1:生成2018-10-03到2018-10-09的日期序列
  
      
示例:列出2015-01-03到2015-01-07每天的销量汇总
MySQL8:
with recursive
t(d,v) as (select date'2015-01-04',30
union all select date'2015-01-06',50
union all select date'2015-01-07',50
union all select date'2015-01-03',40
union all select date'2015-01-04', 80),
s(d) as (select date'2015-01-03'
union all
select d+1 from s where d<date'2015-01-07')
select s.d, sum(t.v) v
from s left join t on s.d=t.d
group by s.d;

集算器 SPL:

A
1
[2015-01-04, 30,   2015-01-06,50, 2015-01-07,50, 2015-01-03,40, 2015-01-04,80]
2
=create(d,v).record(A1)
3
=periods("2015-01-03",   "2015-01-07")
4
=A2.align@a(A3,d)
5
=A4.new(A3(#):d,   ~.sum(v):v)
A4:A2中记录按字段d的值对齐到A3
A5:根据A4和A3对位构造统计后的序表

3、    生成连续的工作日(不包含周六周日)序列
MySQL8:
with recursive
t(d) as (select date'2018-10-03'
union all
select d+1 from t where d<date'2018-10-09')
select d,dayofweek(d) w from t
where dayofweek(d)<=5;
集算器 SPL:

A
1
=workdays(date("2018-10-03"),   date("2018-10-09"))
2
=A1.new(~:d,day@w(~)-1:w)
       A1:构造从2018-10-03到2018-10-09不包含周六周日的日期序列
       A2:根据A1构造日期及相应周几的序表
      
4、    根据序列生成表
MySQL8:
with recursive t1(n) as (select 1 union all select n+1 from t1 where n<14),
t2(n, name) as (select n, concat('a',n) name from t1)
select max(if(n%4=1, name, null)) f1,
max(if(n%4=2, name, null)) f2,
max(if(n%4=3, name, null)) f3,
max(if(n%4=0, name, null)) f4
from t2
group by floor((n+3)/4);
集算器 SPL:

A
1
=to(14).("a"/~)
2
=create(f1,f2,f3,f4).record(A1)

来源:润乾软件

楼主热帖
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖 赞 踩

168大数据 - 论坛版权1.本主题所有言论和图片纯属网友个人见解,与本站立场无关
2.本站所有主题由网友自行投稿发布。若为首发或独家,该帖子作者与168大数据享有帖子相关版权。
3.其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和168大数据的同意,并添加本文出处。
4.本站所收集的部分公开资料来源于网络,转载目的在于传递价值及用于交流学习,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。
5.任何通过此网页连接而得到的资讯、产品及服务,本站概不负责,亦不负任何法律责任。
6.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源,若标注有误或遗漏而侵犯到任何版权问题,请尽快告知,本站将及时删除。
7.168大数据管理员和版主有权不事先通知发贴者而删除本文。

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

关于我们|小黑屋|Archiver|168大数据 ( 京ICP备14035423号|申请友情链接

GMT+8, 2024-4-29 07:29

Powered by BI168大数据社区

© 2012-2014 168大数据

快速回复 返回顶部 返回列表