StockPrice Table
price_date
price
2019-09-25
23.23
2019-09-26
32.54
2019-09-27
34.55
2019-09-28
null
2019-09-29
null
2019-09-30
54.22
2019-10-01
null
2019-10-02
null
2019-10-03
null
2019-10-04
null
2019-10-05
null
2019-10-06
null
2019-10-07
null
2019-10-08
33.80
2019-10-09
33.83
上表节选自某上市公司股票每日收盘价记录,适逢股市休市或该股停牌,则无收盘价记录,于表中记为null。
现需通过sql获取该公司在第a天的收盘价,若第a天无收盘价记录,则取该日之前,最近的一个收盘价作为第a天的收盘价。(例如,2019-10-05的收盘价取2019-09-30的54.22)
select
price_date,
price,
max(newprice) over (partition by num) close_price
from
(
select
price_date,
price,
newprice,
sum(c) over ( UNBOUNDED PRECEDING) num
from
(
select
price_date,
price,
if(price = null,0,price) newprice,
if(price = null,0,1) c
from
`StockPrice Table`
) t1
)t2
改了下让求距离该日期最近的,就是说可以向前取也可以向后取,说如果求最近得价格,如果为null,就向前一天和后一天找,如果找到一个不为null且距离该日期最近的,比如:2019.5.12为null,2019.5.10是32.2,2019.5.13是23.0,那2019.5.12就是23.0.因为对于5.12来说5.13比5.10近
这个需求改变了,怎么改SQL?
select t3.price_date,d.price
from (
select t.price_date,case when datediff(price_date,price_date2)>datediff(price_date3,price_date) then price_date3 else price_date2 end price_date4
from (
select t.price_date,t.price_date2,min(c.price_date) price_date3
from (
select a.price_date,max(b.price_date) price_date2
from `StockPrice Table` a
left join (select * from `StockPrice Table` where a.price is not null) b
on a.price_date>=b.price_date
group by a.price_date
) t
left join (select * from `StockPrice Table` where a.price is not null) c
on t.price_date<=c.price_date
group by t.price_date,t.price_date2
) t2 ) t3
join `StockPrice Table` d
on t3.price_date4=d.price_date
--日期间隔函数不同sql 会有不同
提供个思路:
1.从表中求出不为null的所有日期与你跑的数据日期的差值绝对值
select 日期,日期-你的数据日期,ABS(日期-你的数据日期) from table where 收盘价 is not null
2.根据绝对值排序,取最小的日期,然后再用日期去关联表就出来了
注意:
如果当天为空,前一天和后一天都不为空的,取前一天还是后一天自己处理下就行了,这个在第二步排序的时候按两个字段排序就行了,绝对值、差值 (一个为正,一个为负,自己按需拍升序或降序)
select price_date,price from(
select price_date,price,abs(DATEDIFF(day,t1.price_date,你的数据日期)) 天数 from
StockPrice
where price_date between
isnull( (select max(price_date) as price_date from StockPrice where price_date<你的数据日期 and price>0 ),你的数据日期)
and
isnull( (select min(price_date) as price_date from StockPrice where price_date>你的数据日期 and price>0 ),你的数据日期) ) t1
where 天数=(select MIN(abs(DATEDIFF(day,t1.price_date,你的数据日期))) from
StockPrice
where price_date between
isnull( (select max(price_date) as price_date from StockPrice where price_date<你的数据日期 and price>0 ),你的数据日期)
and
isnull( (select min(price_date) as price_date from StockPrice where price_date>你的数据日期 and price>0 ),你的数据日期)
)
第二套
自己加上最小天数条件
哪个速度快用哪个
select price_date,price,0 as 天数
from StockPrice t1
where price_date=你的数据日期
union all
select t1.price_date,t1.price,DATEDIFF(day,你的数据日期,t1.price_date) as 天数
from StockPrice t1
inner join (select max(price_date) as price_date from StockPrice where price_date<你的数据日期 and price>0 ) t2
on t1.price_date=t2.price_date
union all
select t1.price_date,t1.price,DATEDIFF(day,t1.price_date,你的数据日期) 天数
from StockPrice t1
inner join (select min(price_date) as price_date from StockPrice where price_date>你的数据日期 and price>0 ) t2
on t1.price_date=t2.price_date