0

假设我有一个Transaction具有以下字段的模型[token, pair_token, amount, related_transaction],我想在 mysql 中生成这样的查询:

SELECT token_id, pair_token_id, (
  SELECT ABS(t3.amount / t2.amount) price
    FROM 
    Transaction t2 join Transaction t3 on t2.related_transaction_id=t3.id 
    WHERE t2.id=MAX(t1.id)) 
FROM 
Transaction t1 
WHERE 
t1.token_id in (1, 2, 3, 5, 6) and t2.pair_token_id in (4) and t1.timestamp > CURRENT_TIMESTAMP - interval 24 hour 
GROUP BY
token_id, pair_token_id;

此查询查找两个相关交易的最后一个值,该值等于令牌及其 pair_token 组合的价格。为了在 django 中得到类似的结果,我使用Subquery如下:

trs = Transaction.objects.filter(token_id__in=[1, 2, 3], pair_token_id__in=[4], timestamp__gt=Now()-timedelta(hours=24))
last_price = Transaction.objects.annotate(price=Abs(F('related_transaction__amount') / F('amount')))
trs = trs.values('token_id', 'pair_token_id').annotate(
    price=Subquery(last_price.filter(id=Max(OuterRef('id'))).values('price'))).\
values('token_id', 'pair_token_id', 'price')

但它会生成错误的查询:

SELECT
`Transaction`.`token_id`, `Transaction`.`pair_token_id`, (
        SELECT ABS((U1.`amount` / U0.`amount`)) AS `price` 
        FROM 
        `Transaction` U0 LEFT OUTER JOIN `Transaction` U1 ON (U0.`related_transaction_id` =U1.`id`) 
        HAVING U0.`id` = MAX(`Transaction`.`id`)) AS `price` 
FROM 
`Transaction` 
WHERE (`Transaction`.`pair_token_id` IN (4) AND `Transaction`.`timestamp` > (CURRENT_TIMESTAMP - INTERVAL 86400000000 MICROSECOND) AND `Transaction`.`token_id` IN (1, 2, 3, 5, 6))

Mysql 为这个查询生成一个错误,它必须是,我不知道如何避免having在 using 中生成一个查询Subquery。如果我在没有任何Subquerygroup by 子句的情况下使用此查询,则会生成但在使用Subquerygroup by term 时将被删除并having出现。

我正在使用django 3.1.1mysql 8.0.19

更新

交易模式:

class Token:
    name = models.CharField(max_length=20)

class Transaction:
    token = models.ForeignKey(
        Token, blank=False, null=False, on_delete=models.CASCADE
    )
    pair_token = models.ForeignKey(
        Token, blank=True, null=True, on_delete=models.SET_NULL
    )

    related_transaction = models.ForeignKey(
        Transaction, blank=True, null=True, on_delete=models.PROTECT
    )

    amount = models.DecimalFeild(
        max_digits=10, price_decimals=3
    )


t1 = Token.objects.create(name='T1')
t2 = Token.objects.create(name='T2')
t3 = Token.objects.create(name='T3')

tr11 = Transaction.objects.create(
    token=t1, pair_token=t2, amount=Decimal('2.4')
)

tr12 = Transaction.obejcts.create(
    token=t2, pair_token=t1, related_transaction=tr11, amount=Decimal('3')
)

tr21 = Transaction.objects.create(
    token=t1, pair_token=t2, amount=Decimal('1.4')
)

tr22 = Transaction.obejcts.create(
    token=t2, pair_token=t1, related_transaction=tr21, amount=Decimal('3')
)


# If I want to get price of t2 in t1 I must divide tr21.amount / tr22.amount
# Notice that there are many transactions related to each pair and I want to just list of the last price of each pairs.  
4

1 回答 1

1

下面应该给你你想要的结果。

首先,我们创建一个计算交易价格的子查询,通过外部令牌/pair_token 进行过滤,然后进行排序,以便最大 id 排在第一位

latest_prices = Transaction.objects.filter(
    related_transaction__isnull=False
).annotate(
    price=Abs(F('related_transaction__amount') / F('amount'))
).filter(
    token=OuterRef('token'),
    pair_token=OuterRef('pair_token')
).order_by('-id')

然后使用这个子查询用 token/pair_token 的最新价格注释每一行,并使用它distinct来获取唯一值

Transaction.objects.filter(
    related_transaction__isnull=False
).annotate(
    price=Subquery(latest_prices.values('price')[:1])
).values('token_id', 'pair_token_id', 'price').distinct()
于 2021-04-18T17:19:46.027 回答