Sql Server从日行情表中获取周或月行情

需求: 已经有证券日行情表, 需要获取周行情表和月行情表

本来想用Python的pandas库写的, 但是想要获取价格变化Change和Chg, 会有点难度, 这涉及到groupby分组之后的处理

        sql = """
        select b.Secid,b.Symbol,b.[Open],a.[High],a.[Low],c.[Close],a.Volume,a.Turnover,(c.[Close] - b.[Open]) as Change,round((c.[Close] - b.[Open])/nullif(b.[Open],0),4)*100 as Chg from
        (select secid,max(High) as High, min(Low) as Low,sum(Volume) as Volume,sum(Turnover) as Turnover from hk_StockHistoricaldaily
         where turnover is not null and date between '%(date)s' and '%(week_end)s' group by secid) a
        cross apply
        (select top 1 * from hk_StockHistoricaldaily where secid=a.secid and turnover is not null and date between '%(date)s' and '%(week_end)s' order by date) b
        cross apply
        (select top 1 * from hk_StockHistoricaldaily where secid=a.secid and turnover is not null and date between '%(date)s' and '%(week_end)s' order by date desc) c
        """ % {"date": date, "week_end": week_end}

以周行情为例:

传入的两个参数分别是一周的开始日和结束日, 只要包含一周的全部交易日即可.

猜你喜欢

转载自www.cnblogs.com/chendongblog/p/12501041.html