我做了一個腳本,每隔幾分鐘從網站下載股票價格。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
如何在group by子句中添加日期以及。 –
你想不管一天還是一天都按時間價格嗎? – xQbert
請編輯您的問題並提供期望的結果。我無法弄清楚這應該是什麼意思:「返回最高出價,最低要求,平均要求和出價」FAV「在他們下載的那一刻」。 –