SQL难点解决:集合及行号_邹小青的博客-程序员秘密

技术标签: 集合  Mysql  行号  Oracle  sql  

相关文章:

《SQL难点解决:记录的引用》

《SQL 难点解决:直观分组》

《SQL 难点解决:序列生成》

《MySQL难点解决:实现Oracle高级分析函数》

《MySQL难点解决:窗口函数》

 

1. 和集

示例1:求重叠时间段的总天数

MySQL8:

with recursive t(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'),

t1(d,end) as (select start,end from t

union all select d+1,end from t1 where d<end)

select count(distinct d) from t1;

说明:此例先将各时间段转成时间段内所有日子对应的日期,然后再求不同日期的个数

 

集算器SPL:

 

A

1

=connect("mysql")

2

[email protected]("select date'2010-01-07' start,date'2010-01-9' end 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'")

3

=A2.(periods(start,end))

4

=A3.conj()

5

=A4.icount()

A3: 对A2中的每一个时间段构造从start到end的日期序列

A4: 求A3中所有日期序列的和

A5: 求A4中不重复日期的个数

 

2. 差集

示例1:列出英语人口和法语人口均超过5%的国家

MySQL8:

with t1(lang) as (select 'English' union all select 'French')

select name from world.country c

where not exists(select * from t1 where lang not in (select language from world.countrylanguage where percentage>=5 and countrycode=c.code));

说明:此SQL只是演示通过双重否定实现差集为空

 

集算器SPL:

 

A

1

=connect("mysql")

2

=A1.query("select CountryCode,Name,Language,Percentage from world.countrylanguage cl join world.country c on cl.countrycode=c.code where percentage>5")

3

=A2.group(CountryCode)

4

=A3.select(["English","French"]\~.(Language)==[])

5

=A4.new(~.Name:name)

A4: 选出[“English”,”French”]与本组语言集合的差为空的组,意思就是选出语言集合包含English和French的组

 

3. 交集

示例1:列出英语人口、法语人口、西班牙语人口分别超过0.3%、0.2%、0.1%的国家代码

MySQL8:

with t1 as (select countrycode from world.countrylanguage where language='English' and percentage>0.3),

t2 as (select countrycode from world.countrylanguage where language='French' and percentage>0.2),

t3 as (select countrycode from world.countrylanguage where language='Spanish' and percentage>0.1)

select countrycode

from t1 join t2 using(countrycode) join t3 using(countrycode);

说明:此例只是演示如何求解多个集合的交集

 

集算器SPL:

 

A

1

=connect("mysql")

2

[English,French,Spanish]

3

[0.3,0.2,0.1]

4

=A2.([email protected]("select countrycode from world.countrylanguage where language=? and percentage>?",~,A3(#)))

5

>A1.close()

6

=A4.isect()

A3: 按次序依次查询英语人口超0.3%、法语人口超0.2%、西班牙语超0.1%的国家代码,并转成序列

A5: A3中所有序列交集

 

4. 根据行号取数据

示例1: 计算招商银行(600036) 2017年第3个交易日和倒数第3个交易日的交易信息

MySQL8:

with t as (select *, row_number() over(order by tdate) rn from stktrade where sid='600036' and tdate between '2017-01-01' and '2017-12-31')

select tdate,open,close,volume from t where rn=3

union all

select tdate,open,close,volume from t where rn=(select max(rn)-2 from t);

 

集算器SPL:

 

A

1

=connect("mysql")

2

[email protected]("select * from stktrade where sid='600036' and tdate between '2017-01-01' and '2017-12-31' order by tdate ")

3

=A2(3)|A2.m(-3)

A3: 第3条记录和倒数第3条记录的和集

 

示例2计算招商银行(600036)最近20个交易日的平均收盘价

MySQL8:

with t as (select *, row_number() over(order by tdate desc) rn from stktrade where sid='600036')

select avg(close) avg20 from t where rn<=20;

 

集算器SPL:

 

A

1

=connect("mysql")

2

[email protected]("select * from stktrade where sid='600036' order by tdate")

3

=A2.m(-20:)

4

=A3.avg(close)

A2: 将600036的交易记录按日期排序

A3: 取从倒数20条到末尾的所有记录

A4: 求A3中所有记录收盘价的平均值

 

5. 求满足条件的记录的行号

示例1:计算招商银行(600036)2017年经过多少交易日收盘价达到25元

MySQL8:

with t as (select *, row_number() over(order by tdate) rn from stktrade where sid='600036' and tdate between '2017-01-01' and '2017-12-31')

select min(rn) from t where close>=25;

 

集算器SPL:

 

A

1

=connect("mysql")

2

[email protected]("select * from stktrade where sid='600036' and tdate between '2017-01-01' and '2017-12-31' order by tdate ")

3

=A2.pselect(close>=25)

A3: 从前往后查找第1个收盘价达到25元的记录位置

 

示例2计算格力电器(000651) 2017年涨幅(考虑停牌)

MySQL8:

with t as (select * from stktrade where sid='000651'),

t1(d) as (select max(tdate) from t where tdate<'2017-01-01'),

t2(d) as (select max(tdate) from t where tdate<'2018-01-01')

select s2.close/s1.close-1 rise

from (select * from t,t1 where tdate=d) s1,

(select * from t,t2 where tdate=d) s2;

 

集算器SPL:

 

A

1

=connect("mysql")

2

[email protected]("select * from stktrade where sid='000651' and tdate<'2018-01-01' order by tdate ")

3

[email protected](tdate<date("2017-01-01"))

4

=A2(A3).close

5

=A2.m(-1).close

6

=A5/A4-1

A2: 数据按交易日从小到大排序

A3: 从后往前查找交易日在2017-01-01之前的最后一条记录在序列中的行号

A4: 求2016年收盘价

A5: 求2017年收盘价,其中A2.m(-1)取倒数第1条记录,即2017年最后一个交易日对应的记录

 

示例3列出2017年信息发展(300469)交易量超过250万股时的交易信息及各日涨幅(考虑停牌)

MySQL8:

with t as (select *, row_number() over(order by tdate) rn

from stktrade where sid='300469' and tdate<=date '2017-12-31'),

t1 as (select * from t where tdate>=date'2017-01-01' and volume>=2500000)

select t1.tdate, t1.close, t.volume, t1.close/t.close-1 rise

from t1 join t on t1.rn=t.rn+1;

 

集算器SPL:

 

A

1

=connect("mysql")

2

[email protected]("select * from stktrade where sid='300469' and tdate<= date'2017-12-31' order by tdate ")

3

[email protected](tdate>=date("2017-01-01") && volume>2500000)

4

=A3.new(A2(~).tdate:tdate, A2(~).close:close, A2(~).volume:volume, A2(~).close/A2(~-1).close-1:rise)

A3: 求出2017年交易量超250万股所有记录的行号

A4: 根据行号计算相应的日期、收盘价、交易量、涨幅

 

6. 求最大值或最小值所在记录的行号

示例1: 计算招商银行(600036) 2017年最早的最低价与最早的最高价间隔多少交易日

MySQL8:

with t as (select *, row_number() over(order by tdate) rn from stktrade where sid='600036' and tdate between '2017-01-01' and '2017-12-31'),

t1 as (select * from t where close=(select min(close) from t)),

t2 as (select * from t where close=(select max(close) from t))

select abs(cast(min(t1.rn) as signed)-cast(min(t2.rn) as signed)) inteval

from t1,t2;

 

集算器SPL:

 

A

1

=connect("mysql")

2

[email protected]("select * from stktrade where sid='600036' and tdate between '2017-01-01' and '2017-12-31' order by tdate ")

3

=A2.pmax(close)

4

=A2.pmin(close)

5

=abs(A3-A4)

A3: 从前往后找最大收盘价在序列中的行号

A4: 从前往后找最小收盘价在序列中的行号

 

示例2计算招商银行(600036) 2017年最后的最低价与最后的最高价间隔多少交易日

MySQL8:

with t as (select *, row_number() over(order by tdate) rn from stktrade where sid='600036' and tdate between '2017-01-01' and '2017-12-31'),

t1 as (select * from t where close=(select min(close) from t)),

t2 as (select * from t where close=(select max(close) from t))

select abs(cast(max(t1.rn) as signed)-cast(max(t2.rn) as signed)) inteval

from t1,t2;

 

集算器SPL:

 

A

1

=connect("mysql")

2

[email protected]("select * from stktrade where sid='600036' and tdate between '2017-01-01' and '2017-12-31' order by tdate ")

3

[email protected](close)

4

[email protected](close)

5

=abs(A3-A4)

A3: 从后往前找最大收盘价在序列中的行号

A4: 从后往前找最小收盘价在序列中的行号

 

7. 有序集合间的对位计算

示例1:求2018年3月6日到8日创业板指(399006)对深证成指(399001)的每日相对收益率

MySQL8:

with t1 as (select *,close/lag(close) over(order by tdate) rise from stktrade where sid='399006' and tdate between '2018-03-05' and '2018-03-08'),

t2 as (select *, close/lag(close) over(order by tdate) rise from stktrade where sid='399001' and tdate between '2018-03-05' and '2018-03-08')

select t1.rise-t2.rise

from t1 join t2 using(tdate)

where t1.rise is not null;

 

集算器SPL:

 

A

1

=connect("mysql")

2

=["399006","399001"].(A1.query("select * from stktrade where sid=? and tdate between '2018-03-05' and '2018-03-08'",~))

3

>A1.close()

4

=A2.(~.calc(to(2,4),close/close[-1]))

5

=A4(1)--A4(2)

A2: 依次查询399006和399001从2018年3月5日到8日的交易数据

A4: 依次计算A2中2个序表从第2条记录到第4条记录的涨幅,也就是399006和399001从2018年3月6日到8日的每天涨幅

A5: 对位相减,即可算出每日相对收益率

 

相关文章:

《SQL难点解决:记录的引用》

《SQL 难点解决:直观分组》

《SQL 难点解决:序列生成》

《MySQL难点解决:实现Oracle高级分析函数》

《MySQL难点解决:窗口函数》

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_43678764/article/details/85054859

智能推荐

Python import 其他文件夹下的模块_zorro721的博客-程序员秘密

如果要import不在同一个路径下的module,则需要先把这个module的路径添加进来,示例: import try文件夹下的module fun 代码:from sys import pathpath.append(r'D:\python_code\try') #将存放module的路径添加进来import fun #引入模块

MYSQL创建数据表时日期字段默认插入当前时间_weixin_30731287的博客-程序员秘密

创建数据库表的时候,日期字段通常都是默认当前插入时间。在SQL SERVER中,日期的datetime类型可以直接使用函数getdate()来定义默认值。但是在MYSQL中的datetime类型不可以这么用。所以在MYSQL中,如果想要插入时自动获取当前时间,则需要使用timestamp类型,然后赋默认值就可以了。例如:postTime timestamp defa...

SMPL|论文笔记(持续输出......)_憨憨冲鸭的博客-程序员秘密

SMPL论文vertices顶点:N=6890,joints关节点:K=23;mean template shape平均模板形状表示,其中包括:连接顶点的零姿态向量:零姿态:混合权重:基于上述参数,SMPL建模的可视化结果如下图:混合形状函数:该函数输入为形状参数beta,输出塑造id的混合shape。预测关节点位置函数(注意维度变化):基于上述参数,SMPL建模的模型如下图所示:Note:在形状矢量beta中,顶点和关节的位置是线性的。一个依赖姿态的混合

python处理多个excel数据_python 读取多个excel数据写入同一个excel中_摩方智享的博客-程序员秘密

一、将excel数据源存储到同一个excel的不同sheet 中例如A.xlsx 中有test1,test2 两张sheet。B.xlsx中有test3 一张sheet,存储到C.xlsx中后,C有三张sheet,分别是test1,test2,test3 代码:file_directory = 'D:/test' # 存放要合并的数据源文件路径writer = pd.ExcelWriter(...

阿里云oss图片不能预览_赤心的博客-程序员秘密

阿里云2020年新发布的通知中需要自定义域名,使用默认域名时为下载设置 HTTP 头 :Content-Disposition设置为inline(inline :将文件内容直接显示在页面 attachment:弹出对话框让用户下载 )

C++ ZeroMemory和memset_weixin_34318272的博客-程序员秘密

为什么80%的码农都做不了架构师?&gt;&gt;&gt; ...

随便推点

微信小程序仿网易云音乐(使用云开发,提供源码)_仿网易云音乐小程序源码_小兔崽啊啊啊的博客-程序员秘密

源码文章目录前言一、实现页面(1)云村首页(2)云村中的云圈(3)歌曲搜索功能(4)达人页面(5)手机型号自适应性二、源码文件打开方式(1)修改_openid(2)打开云数据库并导入数据三、总结(1)通过数据库查询实现路由切换菜单栏(2)navigator跳转页面(3)点击实现页面跳转并传参(4)实现弹幕,视频的播放(5)对数据库的查询(6)swiper轮播图(7)简单的移动(8)css实现左侧边栏(9)瞄点实现分类菜单栏(10)点击隐藏出现前言一、实现页面(1)云村首页(2)云村中的云圈

最新版docker安装,更改docker安装位置,镜像数据保存路径,配置镜像加速器_"appsetting \"rootpath\": \"c:\\\\attachments\" do_代码讲故事的博客-程序员秘密

最新版docker安装,更改docker安装位置,镜像数据保存路径,配置镜像加速器。一、下载根据自己的平台和环境,选择对应的版本进行下载和部署,这里提供官方最新的地址。点击进入二、安装这一步并没有什么注意的地方,windows系统双击安装,Linux/MacOS通过双击或者命令行安装都很简单。唯一需要注意的是,Linux/MacOS更新一下相关基础依赖,Windows系统安装之前更新一下 wsl2,在下方地址进行更新:点击进入Linux安装:Linux发行版Ubuntu安装方法切换到管

程序员面试宝典:输入一行字符串,找出其中出现的相同且长度最长的字符串----后缀数组求解..._weixin_30737363的博客-程序员秘密

更多关于最长子串:http://blog.csdn.net/szu030606/article/details/8088596#include&lt;iostream&gt;#include&lt;string&gt;#include&lt;string.h&gt;using namespace std;int mycmp( const void * a, const void ...

Unity--Vector3的使用_unity vector3赋值_鹅厂程序小哥的博客-程序员秘密

using UnityEngine;public class Vector3Test : MonoBehaviour{ public Transform target; float smoothTime = 1; Vector3 velocity = Vector3.zero; void Start() { // 上下左右,对应坐标...

arcgis for js4.x自定义图例位置添加到地图并导出_arcgis自定义图例_自走棋的博客-程序员秘密

arcgis for js4.x自定义图例位置添加到地图并导出前言arcgis_js版本思路核心代码结果前言许久未写博客了,闲来无事动动手。最近客户有个需求,要求在前端手绘气象落区生成图例,并且能够自定义图例的位置,导出地图。arcgis_js版本arcgis_js_v413_api参考示例:sdk的Print widget和Take a screenshot of a SceneView参考博客:https://blog.csdn.net/weixin_30690833/article/det

vuex状态管理_旭陌小生的博客-程序员秘密

Vuex是什么?Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化。示例:首先我们用vue init webpack vuex-demo生成一个项目然后进入vuex-demo中使用命令:npm i -S vuex下载vuex的依赖包进入项目中的src文件夹下创建一个store.js的文件,为仓库文件然后在store.js文件中使用:import Vue from 'vu

推荐文章

热门文章

相关标签