楼主: 马甲1号
3011 10

请教一条SQL语句 [推广有奖]

  • 3关注
  • 3粉丝

副教授

23%

还不是VIP/贵宾

-

威望
0
论坛币
1948 个
通用积分
8.7693
学术水平
159 点
热心指数
165 点
信用等级
144 点
经验
6272 点
帖子
777
精华
0
在线时间
665 小时
注册时间
2010-10-24
最后登录
2024-4-21

20论坛币
现在有一个数据表A,储存若干股票的日收盘信息。字段规定如下:

序号列名中文名称类型空否备注
1IDIDdecimal(18,0)  
2InnerCode证券内部编码int  
3TradingDay交易日smalldatetime  
4PrevClosePrice昨收盘smallmoney 
5OpenPrice今开盘smallmoney 
6HighPrice最高价smallmoney 
7LowPrice最低价smallmoney 
8ClosePrice收盘价smallmoney 
9TurnoverVolume成交量decimal(20,0) 
10TurnoverValue成交金额(元)decimal(20,0) 
11TurnoverDeals成交笔数int 
12XGRQ更新时间datetime  



现在我想在这个表的基础上生成一个新表,新表保留旧表id、innerCode、TradingDay字段,但是需要新增每只股票每一天的百日新高、新低(Day100Max,Day100Min)与250日新高、新低(Day250Max, Day250Min),请问这个功能用Sql如何实现?谢谢。

最佳答案

lyfyb99 查看完整内容

不用SQL更方便: %macro test; data aa1; set a; %do i=1 %to 99; high&i=lag&i(highprice); low&i=lag&i(lowprice); %end; day100max=max(of highprice high1-high99); day100min=min(of lowprice low1-low99); run; %mend test; %test;
关键词:sql语句 sql turnover datetime decimal 更新时间 成交量 最低价 交易日 收盘价

本帖被以下文库推荐

沙发
lyfyb99 在职认证  发表于 2010-12-29 11:21:14 |只看作者 |坛友微信交流群
不用SQL更方便:
%macro test;
data aa1;
set a;
%do i=1 %to 99;
high&i=lag&i(highprice);
low&i=lag&i(lowprice);
%end;
day100max=max(of  highprice  high1-high99);
day100min=min(of  lowprice  low1-low99);
run;
%mend test;
%test;
已有 1 人评分学术水平 热心指数 收起 理由
马甲1号 + 1 + 1 好的意见建议

总评分: 学术水平 + 1  热心指数 + 1   查看全部评分

使用道具

藤椅
马甲1号 发表于 2010-12-29 11:26:28 |只看作者 |坛友微信交流群
1# 马甲1号

问题补充:所谓“100日”,“250日”是指交易日,而不是自然日。

使用道具

板凳
wahsai 发表于 2010-12-30 15:08:32 |只看作者 |坛友微信交流群
能够实现.. 就是SQL比较复杂

使用道具

报纸
yixufeng 发表于 2010-12-30 15:49:31 |只看作者 |坛友微信交流群
给一个思路:  先限制旧表的时间选取范围(最近一年),然后按照时间倒序排序,取rank=100和rank=250得到100个交易日的初始时间和250个交易日的初始时间,然后统计各自时间范围内的新高、新低即可,至于这个思路的sql效率还有待考虑。主要是用来解决100个交易日和250个交易日问题
已有 1 人评分学术水平 热心指数 收起 理由
马甲1号 + 1 + 1 好的意见建议

总评分: 学术水平 + 1  热心指数 + 1   查看全部评分

使用道具

地板
马甲1号 发表于 2010-12-30 16:14:10 |只看作者 |坛友微信交流群
5# lyfyb99

如果能用sas就好了。Thanks anyway。

使用道具

7
Isscaliu 发表于 2010-12-30 16:34:54 |只看作者 |坛友微信交流群
我的理解是这样的:假如只有102天的交易日,100天新高只能在第100个观测值是观测到第一个100日新高
(max(of 第一天-睇100天)),如此类推,只有三个观测值。
所以以我的理解,很明显那个data step的macro的思路是错误的。但是方法是对的。跑loop求百日新高,然后生成一个data只有交易日和包日新高 然后再跟原来的数据合并。求百日新高的时候要注意的是你时间是向前推的,而lag是向后的,由于本人不是很确定SAS有没有lag-1类似能不能用,建议先sort一下by交易日 descending order 就可以用lag1 到lag100了。
It was the best of times, it was the worst of times.

使用道具

8
littlebig 发表于 2010-12-30 16:47:20 |只看作者 |坛友微信交流群
问个问题先,请问最高和最低价是说收盘价呢还是盘中价

使用道具

9
wannengkey 发表于 2010-12-30 18:45:29 |只看作者 |坛友微信交流群
sql中 用merge+update就可以了
扎扎实实学知识,老老实实去做人!

使用道具

10
yixufeng 发表于 2010-12-31 09:46:50 |只看作者 |坛友微信交流群
insert into B --new table
( id,
  innercode,
  tradingday,
  Day100Max,
  Day100Min,
  Day250Max,
  Day250Min)
with AA as
   (select distict trunc(t.tradingday) as day from A t   -- A is the old table
    where  t.tradingday<trunc(sysdate+1)
    and    t.tradingday>=trunc(add_months(sysdate, -12))   --restrict the time in a year
    ),
   AB as
   (select day, rank()over(order by day desc) rk from AA),
   AC as
   (select day from AB where rk=100),
   AD as
   (select day from AB where rk=250),
   AE as
   (select t.id,max(t.HighPrice) Day100Max,min(t.LowPrice) Day100Min from A t
    where t.tradingday<trunc(sysdate+1)
    and    t.tradingday>=(select day from AC)
    group by t.id
   ),
   AF
   (select t.id,max(t.HighPrice) Day250Max,min(t.LowPrice) Day250Min from A t
    where t.tradingday<trunc(sysdate+1)
    and    t.tradingday>=(select day from AD)
    group by t.id
   )
   select t.id,
   t.innercode,
   t.tradingday,
   tt.Day100Max,
   tt.Day100Min,
   ttt.Day250Max,
   ttt.Day250Min
   from A t , AE tt, AF ttt
   where t.id=tt.id
   and   t.id=ttt.id

使用道具

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

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-28 04:50