SQL:计算库存玻璃内的深度

时间:2016-05-17 07:21:35

标签: sql sqlite

对任务的描述很少:

  • 如果我们将total base卖给coin_quantity,则需要获得coin market
  • 默认情况下,此示例表示marketcoin+"_"+base。没有翻盖。
  • 为简化起见,只有一个案例(行动):selling / ask

蟒:

def get_depth_price( db_cursor, coin_quantity, coin, market ):
            sql_command = "SELECT * FROM '" + market + "' WHERE type=1"
            db_cursor.execute( sql_command )
            bids = sorted([ i for i in  db_cursor.fetchall() ], \
                           key = lambda k: k[3], reverse = True )
            tmp_quantity = float( coin_quantity )
            tmp_total = 0
            for bid in bids:
                    if tmp_quantity > bid[4]:
                            tmp_total += bid[4] * bid[3]
                            tmp_quantity -= bid[4]
                    else:
                            tmp_total += tmp_quantity * bid[3]
                            tmp_quantity = 0
                            break
            return tmp_total

市场数据如下:

# echo "SELECT * from 'DOGE_BTC' WHERE type=1 
        ORDER BY price DESC;" | sqlite3 big.db

6f827564d88ddd0d99a9f976ac384a3f|0|1|5.0e-07|374365.08
1f696fea1270c07d9d4217e47ad40d3c|0|1|4.9e-07|1337443.42857
b9bee0a3bc2d4b241383062f06569b54|0|1|4.8e-07|465618.375
716cb29e0f5fe4742de73302e5b88250|0|1|4.7e-07|197560.659574
3189ed55c60530014892c6a3fce673e8|0|1|4.6e-07|115757.521739
cf19858241fb25de9095160b1704ef44|0|1|4.5e-07|237807.133333
f53642c0e7d5074daaa2b324e82483c5|0|1|4.4e-07|16112.6818182
ee8fb3f5255fb0ef8c157becb6a8c539|0|1|4.3e-07|22581.0697674
###  (0)id ----^       (1)self---^ ^    ^-(3)price      ^
###     (2)type(ask=0/bid=1)-------+       (4)quantity--+

Methinks,即python脚本 - 对sql来说非常慢。

但是,我无法在sql语言上创建类似的东西。

如何用sql-query替换get_depth_price的完整功能?

如何在类似于if tmp_quantity > bid[4] / then / else的sql上创建?

0 个答案:

没有答案