我的表中有数十亿行。只是粘贴示例数据。
ticker trade_date trade_time Price Volume
---------------------------------------------------------------
AKS 01242017 1025 9.995 75038
AKS 01242017 1030 10 86891
AKS 01242017 1031 9.97 52815
AKS 01242017 1036 10.03 83556
AKS 01242017 1037 10.05 92644
我想按交易时间对数据求和。通常,纽约证券交易所的交易时间为0930到1600小时。
我希望看到以下数据
ticker tdate 1sthour Avg Price 2nd hour Avg price
---------------------------------------------------------------------------
AKS 01242017 161929 9.99 229015 10.02
挑战是我有42000个不同的行情自动收录器,大多数行情自动收录器可能没有每小时的数据。在这种情况下,我要在没有售出股票的那一小时显示0。尝试过以总和为例,但结果看起来不太好。
体积的计算方法如下:第一小时930-1030 = 75038 + 86891 = 169129 平均价格=(75038 * 9.995 + 86891 * 10)/(75038 + 86891)
答案 0 :(得分:0)
我认为这将满足您的要求
select ticker, tdate,
sum(case when tradetime >= '0930' and tradetime <= '1030' then volume else 0 end) as volume_1,
avg(case when tradetime >= '0930' and tradetime <= '1030' then price end) as price_1,
sum(case when tradetime > '1030' and tradetime <= '1130' then volume else 0 end) as volume_2,
avg(case when tradetime >= '1030' and tradetime <= '1130' then price end) as price_2,
. . .
from t
group by ticker, tdate ;