股票 SQL

insert into stock.dm_stock_trade_total_result

select

'20230928'

,ts_code

,max(case when to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) = to_date('2023-09-28') then close end)

,min(case when to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) <= to_date('2023-09-28') and to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) >= to_date(date_sub('2023-09-28',365)) then cast(close as float) end)

,max(case when to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) <= to_date('2023-09-28') and to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) >= to_date(date_sub('2023-09-28',365)) then cast(close as float) end)

,min(case when to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) <= to_date('2023-09-28') and to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) >= to_date(date_sub('2023-09-28',183)) then cast(close as float) end)

,max(case when to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) <= to_date('2023-09-28') and to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) >= to_date(date_sub('2023-09-28',183)) then cast(close as float) end)

,min(case when to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) <= to_date('2023-09-28') and to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) >= to_date(date_sub('2023-09-28',31)) then cast(close as float) end)

,max(case when to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) <= to_date('2023-09-28') and to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) >= to_date(date_sub('2023-09-28',31)) then cast(close as float) end)

,min(case when to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) <= to_date('2023-09-28') and to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) >= to_date(date_sub('2023-09-28',7)) then cast(close as float) end)

,max(case when to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) <= to_date('2023-09-28') and to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) >= to_date(date_sub('2023-09-28',7)) then cast(close as float) end)

,sum(case when to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) <= to_date('2023-09-28') and to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) >= to_date(date_sub('2023-09-28',365)) then 1 end)

,sum(case when to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) <= to_date('2023-09-28') and to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) >= to_date(date_sub('2023-09-28',365)) and status = '1' then 1 end)

,sum(case when to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) <= to_date('2023-09-28') and to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) >= to_date(date_sub('2023-09-28',365)) and status = '0' then 1 end)

,sum(case when to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) <= to_date('2023-09-28') and to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) >= to_date(date_sub('2023-09-28',183)) then 1 end)

,sum(case when to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) <= to_date('2023-09-28') and to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) >= to_date(date_sub('2023-09-28',183)) and status = '1' then 1 end)

,sum(case when to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) <= to_date('2023-09-28') and to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) >= to_date(date_sub('2023-09-28',183)) and status = '0' then 1 end)

,sum(case when to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) <= to_date('2023-09-28') and to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) >= to_date(date_sub('2023-09-28',31)) then 1 end)

,sum(case when to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) <= to_date('2023-09-28') and to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) >= to_date(date_sub('2023-09-28',31)) and status = '1' then 1 end)

,sum(case when to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) <= to_date('2023-09-28') and to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) >= to_date(date_sub('2023-09-28',31)) and status = '0' then 1 end)

,sum(case when to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) <= to_date('2023-09-28') and to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) >= to_date(date_sub('2023-09-28',7)) then 1 end)

,sum(case when to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) <= to_date('2023-09-28') and to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) >= to_date(date_sub('2023-09-28',7)) and status = '1' then 1 end)

,sum(case when to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) <= to_date('2023-09-28') and to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) >= to_date(date_sub('2023-09-28',7)) and status = '0' then 1 end)

,sum(case when to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) <= to_date('2023-09-28') and to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) >= to_date(date_sub('2023-09-28',14)) then 1 end)

,sum(case when to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) <= to_date('2023-09-28') and to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) >= to_date(date_sub('2023-09-28',14)) and status = '1' then 1 end)

,sum(case when to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) <= to_date('2023-09-28') and to_date(concat(substr(trade_date,1,4),'-',substr(trade_date,5,2),'-',substr(trade_date,7,2))) >= to_date(date_sub('2023-09-28',14)) and status = '0' then 1 end)

,'20230928'

from stock.dm_stock_trade_d

group by ts_code

select * from stock.dm_stock_trade_total_result

--------------------------------两年

select

ts_code

,max(case when substr(trade_date,1,6) = '202201' then up end) 202201_up

,max(case when substr(trade_date,1,6) = '202202' then up end) 202202_up

,max(case when substr(trade_date,1,6) = '202203' then up end) 202203_up

,max(case when substr(trade_date,1,6) = '202204' then up end) 202204_up

,max(case when substr(trade_date,1,6) = '202205' then up end) 202205_up

,max(case when substr(trade_date,1,6) = '202206' then up end) 202206_up

,max(case when substr(trade_date,1,6) = '202207' then up end) 202207_up

,max(case when substr(trade_date,1,6) = '202208' then up end) 202208_up

,max(case when substr(trade_date,1,6) = '202209' then up end) 202209_up

,max(case when substr(trade_date,1,6) = '202210' then up end) 202210_up

,max(case when substr(trade_date,1,6) = '202211' then up end) 202211_up

,max(case when substr(trade_date,1,6) = '202212' then up end) 202212_up

,max(case when substr(trade_date,1,6) = '202301' then up end) 202301_up

,max(case when substr(trade_date,1,6) = '202302' then up end) 202302_up

,max(case when substr(trade_date,1,6) = '202303' then up end) 202303_up

,max(case when substr(trade_date,1,6) = '202304' then up end) 202304_up

,max(case when substr(trade_date,1,6) = '202305' then up end) 202305_up

,max(case when substr(trade_date,1,6) = '202306' then up end) 202306_up

,max(case when substr(trade_date,1,6) = '202307' then up end) 202307_up

,max(case when substr(trade_date,1,6) = '202308' then up end) 202308_up

,max(case when substr(trade_date,1,6) = '202309' then up end) 202309_up

from (

select

substr(trade_date,1,6) trade_date

,ts_code

,sum(case when status = 0 then 1 end) down

,sum(case when status = 1 then 1 end) up

from stock.dm_stock_trade_d where ts_code='300730.SZ'

group by substr(trade_date,1,6),ts_code

) t

group by ts_code


select

ts_code

,max(case when substr(trade_date,1,6) = '202301' then down end) 202301_down

,max(case when substr(trade_date,1,6) = '202301' then up end) 202301_up

,max(case when substr(trade_date,1,6) = '202302' then down end) 202302_down

,max(case when substr(trade_date,1,6) = '202302' then up end) 202302_up

,max(case when substr(trade_date,1,6) = '202303' then down end) 202303_down

,max(case when substr(trade_date,1,6) = '202303' then up end) 202303_up

,max(case when substr(trade_date,1,6) = '202304' then down end) 202304_down

,max(case when substr(trade_date,1,6) = '202304' then up end) 202304_up

,max(case when substr(trade_date,1,6) = '202305' then down end) 202305_down

,max(case when substr(trade_date,1,6) = '202305' then up end) 202305_up

,max(case when substr(trade_date,1,6) = '202306' then down end) 202306_down

,max(case when substr(trade_date,1,6) = '202306' then up end) 202306_up

,max(case when substr(trade_date,1,6) = '202307' then down end) 202307_down

,max(case when substr(trade_date,1,6) = '202307' then up end) 202307_up

,max(case when substr(trade_date,1,6) = '202308' then down end) 202308_down

,max(case when substr(trade_date,1,6) = '202308' then up end) 202308_up

from (

select

substr(trade_date,1,6) trade_date

,ts_code

,sum(case when status = 0 then 1 end) down

,sum(case when status = 1 then 1 end) up

from stock.dm_stock_trade_d where trade_date >= '20230101' and trade_date <= '20231231' --and ts_code='603008.SH'

group by substr(trade_date,1,6),ts_code

) t

group by ts_code

select

ts_code

,sum(case when status = 0 then 1 end) down

,sum(case when status = 1 then 1 end) up

from stock.dm_stock_trade_d where trade_date >= '20230101' and trade_date <= '20231231'

group by ts_code

相关推荐
aramae10 分钟前
终端之外:解锁Linux命令行的魔法与力量
linux·服务器·apache
郭庆汝13 分钟前
Windows安装java流程
java·windows·android studio
Yvonne爱编码18 分钟前
后端编程开发路径:从入门到精通的系统性探索
java·前端·后端·python·sql·go
迦蓝叶33 分钟前
JAiRouter 0.8.0 发布:Docker 全自动化交付 + 多架构镜像,一键上线不是梦
java·人工智能·网关·docker·ai·架构·自动化
arron88991 小时前
CentOS配置vsftpd服务器
linux·服务器·centos
im_AMBER1 小时前
Leetcode 18 java
java·算法·leetcode
Android技术之家1 小时前
Kotlin与Compose:Android开发的现代化变革
android·java·开发语言·kotlin
失散131 小时前
分布式专题——10.3 ShardingSphere实现原理以及内核解析
java·分布式·架构·shardingsphere·分库分表
十八旬1 小时前
【超简单】后端入门案例-基于SpringBoot+MyBatis-plus+MySQL图书管理系统
java·开发语言·idea·intellij idea·项目实战
GIS之路1 小时前
GDAL 读取遥感影像数据
前端