2017-08-08 13 views
0

私は数分ごとにウェブサイトから株価をダウンロードするスクリプトを作った。SQLグループと計算Avergares

私はダウンロードした瞬間に株式「FAV」の最高入札価格、最低尋ねる平均入札価格、入札単価を返すクエリを作成しようとしていましたが、私はそれらを「時間」でグループ化しようとしました。

+-----+------------+-------+------+---------+----------+-------+ 
| 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 | 
+-----+------------+-------+------+---------+----------+-------+ 

がしかし期待される結果を提供していません。このクエリは、それだけではないの時間で、一日あたりの平均値が得られます。このようなテーブルから

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

:これは私が何をしたいです日:関係なく、一日の...

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';