2017-08-08 57 views
0

我做了一個腳本,每隔幾分鐘從網站下載股票價格。SQL組和計算Avergares

我一直試圖做出一個查詢,返回最高出價,最低要求,平均要求和出價股票「FAV」在他們下載的時刻,我試着按「時間」分組。這就是我想要的:

+------------+--------+------------+-------------+---------+---------+ 
| date | time | lowest_ask | highest_bid | avg_ask | avg_bid | 
+------------+--------+------------+-------------+---------+---------+ 

從這樣一個表:

+-----+------------+-------+------+---------+----------+-------+ 
| id | date | time | type | company | quantity | price | 
+-----+------------+-------+------+---------+----------+-------+ 
| 7 | 2017-08-08 | 94827 | ask | BCQ  | 30000 | 0.4 | 
| 8 | 2017-08-08 | 94827 | ask | BCQ  | 87211 | 0.5 | 
| 9 | 2017-08-08 | 94827 | ask | BCQ  | 10658 | 0.4 | 
| 10 | 2017-08-08 | 94827 | ask | BCQ  | 20000 | 0.39 | 
| 15 | 2017-08-08 | 94827 | ask | FAV  |  2600 | 1.9 | 
| 16 | 2017-08-08 | 94827 | ask | FAV  |  3000 | 1.83 | 
| 17 | 2017-08-08 | 94827 | ask | FAV  |  556 | 1.88 | 
| 18 | 2017-08-08 | 94827 | ask | FAV  |  845 | 1.82 | 
| 19 | 2017-08-08 | 94827 | ask | FAV  |  845 | 1.82 | 
| 20 | 2017-08-08 | 94827 | ask | FAV  |  1249 | 1.9 | 
| 21 | 2017-08-08 | 94827 | ask | FAV  |  4024 | 1.9 | 
| 37 | 2017-08-08 | 94827 | bid | FAV  | 10000 | 1.65 | 
| 38 | 2017-08-08 | 94827 | bid | FAV  |  655 | 1.73 | 
| 39 | 2017-08-08 | 94827 | bid | FAV  |  5000 | 1.7 | 
| 40 | 2017-08-08 | 94827 | bid | FAV  |  2102 | 1.65 | 
| 41 | 2017-08-08 | 94827 | bid | FAV  |  618 | 1.75 | 
| 42 | 2017-08-08 | 94827 | bid | FAV  | 10000 | 1.75 | 
| 43 | 2017-08-08 | 94827 | bid | FAV  |  150 | 1.8 | 
| 52 | 2017-08-08 | 94953 | ask | BCQ  | 30000 | 0.4 | 
| 53 | 2017-08-08 | 94953 | ask | BCQ  | 87211 | 0.4 | 
| 54 | 2017-08-08 | 94953 | ask | BCQ  | 10658 | 0.4 | 
| 55 | 2017-08-08 | 94953 | ask | BCQ  | 20000 | 0.4 | 
| 60 | 2017-08-08 | 94953 | ask | FAV  |  2600 | 1.9 | 
| 61 | 2017-08-08 | 94953 | ask | FAV  |  3000 | 1.83 | 
| 62 | 2017-08-08 | 94953 | ask | FAV  |  556 | 1.88 | 
| 63 | 2017-08-08 | 94953 | ask | FAV  |  845 | 1.82 | 
| 64 | 2017-08-08 | 94953 | ask | FAV  |  845 | 1.82 | 
| 65 | 2017-08-08 | 94953 | ask | FAV  |  1249 | 1.9 | 
| 66 | 2017-08-08 | 94953 | ask | FAV  |  4024 | 1.9 | 
| 82 | 2017-08-08 | 94953 | bid | FAV  | 10000 | 1.65 | 
| 83 | 2017-08-08 | 94953 | bid | FAV  |  655 | 1.73 | 
| 84 | 2017-08-08 | 94953 | bid | FAV  |  5000 | 1.7 | 
| 85 | 2017-08-08 | 94953 | bid | FAV  |  2102 | 1.8 | 
| 86 | 2017-08-08 | 94953 | bid | FAV  |  618 | 1.75 | 
| 87 | 2017-08-08 | 94953 | bid | FAV  | 10000 | 2  | 
| 88 | 2017-08-08 | 94953 | bid | FAV  |  150 | 1.8 | 
| 97 | 2017-08-08 | 95053 | ask | BCQ  | 30000 | 0.45 | 
| 98 | 2017-08-08 | 95053 | ask | BCQ  | 87211 | 0.4 | 
| 99 | 2017-08-08 | 95053 | ask | BCQ  | 10658 | 0.5 | 
| 100 | 2017-08-08 | 95053 | ask | BCQ  | 20000 | 0.4 | 
| 105 | 2017-08-08 | 95053 | ask | FAV  |  2600 | 1.9 | 
| 106 | 2017-08-08 | 95053 | ask | FAV  |  3000 | 1.83 | 
| 107 | 2017-08-08 | 95053 | ask | FAV  |  556 | 1.88 | 
| 108 | 2017-08-08 | 95053 | ask | FAV  |  845 | 1.82 | 
| 109 | 2017-08-08 | 95053 | ask | FAV  |  845 | 1.82 | 
| 110 | 2017-08-08 | 95053 | ask | FAV  |  1249 | 1.9 | 
| 111 | 2017-08-08 | 95053 | ask | FAV  |  4024 | 1.9 | 
| 127 | 2017-08-08 | 95053 | bid | FAV  | 10000 | 1.65 | 
| 128 | 2017-08-08 | 95053 | bid | FAV  |  655 | 1.89 | 
| 129 | 2017-08-08 | 95053 | bid | FAV  |  5000 | 1.7 | 
| 130 | 2017-08-08 | 95053 | bid | FAV  |  2102 | 1.65 | 
| 131 | 2017-08-08 | 95053 | bid | FAV  |  618 | 1.75 | 
| 132 | 2017-08-08 | 95053 | bid | FAV  | 10000 | 1.95 | 
| 133 | 2017-08-08 | 95053 | bid | FAV  |  150 | 1.8 | 
+-----+------------+-------+------+---------+----------+-------+ 

但此查詢不提供預期的結果,它只是給每天的平均值,而不是由時間天:

select 
    date, 
    time, 
    (select min(price) from monitor where type = "ask") as lowest_ask, 
    (select max(price) from monitor where type = "bid") as highest_bid, 
    (select avg(price) from monitor where type = "ask") as avg_ask, 
    (select avg(price) from monitor where type = "bid") as avg_bid 
from monitor where date = date("now") and company = "FAV" 
group by time 
+0

如何在group by子句中添加日期以及。 –

+0

你想不管一天還是一天都按時間價格嗎? – xQbert

+0

請編輯您的問題並提供期望的結果。我無法弄清楚這應該是什麼意思:「返回最高出價,最低要求,平均要求和出價」FAV「在他們下載的那一刻」。 –

回答

0

奇怪的是,當天的不包括在分組但...也許你想看到平均最小/最大在特定的時間......不管一天的?

select 
    date, 
    time, 
    (select min(price), time from monitor z where type = "ask" and Z.time = m.time GROUP BY time) as lowest_ask, 
    (select max(price), time from monitor where type = "bid" and Z.time = m.time GROUP BY time) as highest_bid, 
    (select avg(price), time from monitor where type = "ask" and Z.time = m.time GROUP BY time) as avg_ask, 
    (select avg(price), time from monitor where type = "bid" and Z.time = m.time GROUP BY time) as avg_bid 
from monitor m 
where date = date("now") and company = "FAV" 
group by time 

雖然利用窗口函數有可能是一個更簡潔的方法...

如果你真的需要的日期/時間組..這裏的窗函數的方法:

select 
    date, 
    time, 
    min(case when type = 'ask' then price end) over (partition by date,time) as lowest_Ask 
    max(case when type = 'bid' then price end) over (partition by date,time) as higest_Bid 
    avg(case when type = 'ask' then price end) over (partition by date,time) as avg_ask, 
    avg(case when type = 'bid' then price end) over (partition by date,time) as avg_bid 
from monitor 
where date = date("now") and company = "FAV" 
group by date, time 
0

我的第一個猜測是沒有得到正確的結果是使用子查詢,因爲第一個聚合函數正在計算,然後按「時間」進行分組。可以試試這個:

select 
    date, 
    time, 
    min(price) as lowest_ask, 

    from monitor where date = date("now") and company = "FAV" 
    group by time 
0

您需要包含日期/時間組件。假設你的意圖是在同一天的價值:

select date, time, 
     (select min(m2.price) from monitor m2 where m2.company = m.company and m2.type = 'ask' and m2.date = m.date and m2.time <= m.time) as lowest_ask, 
     (select max(m2.price) from monitor m2 where m2.company = m.company and m2.type = 'bid' and m2.date = m.date and m2.time <= m.time) as highest_bid, 
     (select avg(m2.price) from monitor m2 where m2.company = m.company and m2.type = 'ask' and m2.date = m.date and m2.time <= m.time) as avg_ask, 
     (select avg(m2.price) from monitor m2 where m2.company = m.company and m2.type = 'bid' and m2.date = m.date and m2.time <= m.time) as avg_bid 
from monitor m 
where m.date = date("now") and m.company = 'FAV';