如何将此SQL转换为Django查询?

时间:2018-05-08 15:43:33

标签: python mysql django

我正在编写Python / Django应用程序来进行一些股票分析。

我有两个非常简单的模型,如下所示:

class Stock(models.Model):
    symbol = models.CharField(db_index=True, max_length=5, null=False, editable=False, unique=True)

class StockHistory(models.Model):
    stock = models.ForeignKey(Stock, related_name='StockHistory_stock', editable=False)
    trading_date = models.DateField(db_index=True, null=False, editable=False)
    close = models.DecimalField(max_digits=12, db_index=True, decimal_places=5, null=False, editable=False)

    class Meta:
        unique_together = ('stock', 'trading_date')

这是我填充的虚拟数据:

import datetime
a = Stock.objects.create(symbol='A')
b = Stock.objects.create(symbol='B')
c = Stock.objects.create(symbol='C')
d = Stock.objects.create(symbol='D')

StockHistory.objects.create(trading_date=datetime.date(2018,1,1), close=200, stock=a)
StockHistory.objects.create(trading_date=datetime.date(2018,1,2), close=150, stock=a)
StockHistory.objects.create(trading_date=datetime.date(2018,1,3), close=120, stock=a)
StockHistory.objects.create(trading_date=datetime.date(2018,4,28), close=105, stock=a)
StockHistory.objects.create(trading_date=datetime.date(2018,5,3), close=105, stock=a)

StockHistory.objects.create(trading_date=datetime.date(2017,5,2), close=400, stock=b)
StockHistory.objects.create(trading_date=datetime.date(2017,11,11), close=200, stock=b)
StockHistory.objects.create(trading_date=datetime.date(2017,11,12), close=300, stock=b)
StockHistory.objects.create(trading_date=datetime.date(2017,11,13), close=400, stock=b)
StockHistory.objects.create(trading_date=datetime.date(2017,11,14), close=500, stock=b)

StockHistory.objects.create(trading_date=datetime.date(2018,4,28), close=105, stock=c)
StockHistory.objects.create(trading_date=datetime.date(2018,4,29), close=106, stock=c)
StockHistory.objects.create(trading_date=datetime.date(2018,4,30), close=107, stock=c)
StockHistory.objects.create(trading_date=datetime.date(2018,5,1), close=108, stock=c)
StockHistory.objects.create(trading_date=datetime.date(2018,5,2), close=109, stock=c)
StockHistory.objects.create(trading_date=datetime.date(2018,5,3), close=110, stock=c)
StockHistory.objects.create(trading_date=datetime.date(2018,5,4), close=90, stock=c)

我想找到过去一周内年度低点的所有股票。

但是为了使这个问题更简单,假设我想找到'2017-05-04'之后或'2018-04-30'之后发生mysql> select -> s.symbol, -> sh.trading_date, -> low_table.low -> from -> ( -> select -> stock_id, -> min(close) as low -> from -> stocks_stockhistory -> where -> trading_date >= '2017-05-04' -> group by -> stock_id -> ) as low_table, -> stocks_stockhistory as sh, -> stocks_stock as s -> where -> sh.stock_id = low_table.stock_id -> and sh.stock_id = s.id -> and sh.close = low_table.low -> and sh.trading_date >= '2018-04-30' -> order by -> s.symbol asc; +--------+--------------+-----------+ | symbol | trading_date | low | +--------+--------------+-----------+ | A | 2018-05-03 | 105.00000 | | C | 2018-05-04 | 90.00000 | +--------+--------------+-----------+ 2 rows in set (0.02 sec) 以来最低点的所有股票。下面是我写的SQL来找到它。它有效。

但我需要帮助找出要编写的Django Query以获得与此SQL相同的结果。我该怎么办?

context

1 个答案:

答案 0 :(得分:1)

不试图自己构建(这些类型的查询需要一段时间才能考虑)这部分文档应该可以帮助您:Aggregation/Annotation

这些可用于根据QuerySet上的操作结果(例如Min,Avg等)进行复杂查询。您可以使用值(例如'年份低')注释QuerySet,然后根据该值进行过滤。

尝试一些事情,但要记住的一件事是你的Django查询可能看起来不像你的SQL查询 - 正确地获取这些查询可能很困难,但祝你好运!

编辑:Query expressions在这样的查询中也可能有用。