python - 将 SQL 转换为 Django ORM 查询(“Lead”函数内“Window”内的“IF”语句)
问题描述
我正在尝试获取下一个开始使用服务的客户的日期,并将该日期附加到上一个客户。
我试图了解 F() 和 Q() 以及类似 SQL 的函数如何在 Django 中工作,但无法了解如何将以下 SQL 语句转换为有效的 Django 代码:
SELECT
*,
LEAD(
IF(actual_start IS NOT NULL, actual_start, planned_start)
, 1)
OVER(
PARTITION BY customer
ORDER BY actual_start, planned_start
) AS next_starting_time
FROM builds_build;
我尝试将我看到的一些东西混合在一起:
window = {
"partition_by": [F('customer')],
'order_by': [F('actual_start'),F('planned_start')]
}
records = Record.objects.annotate(
next_customer = Window(
expression = Lead(F('actual_start') if F('actual_start') else F('planned_start'),
**window)
)
)
但是,查看查询,没有考虑 LEAD 表达式的条件部分(这是 Django 生成的 SQL 的样子):
SELECT
*,
LEAD(actual_start, 1)
OVER(
PARTITION BY customer
ORDER BY actual_start, planned_start
) AS next_starting_time
FROM builds_build;
但是,它完全删除了预期的“IF”语句。任何提示和想法将不胜感激
解决方案
推荐阅读
- swagger - 大摇大摆的模式数组
- for-loop - 嵌套 For 循环 astrix 模式
- firebase - 在查询中使用 FieldPath 设置 redux-firestore 监听器
- python-3.x - 在ubuntu上没有pip的python安装
- javascript - 如果表格行直到下一个表格标题被隐藏,则隐藏表格标题
- javascript - 字典的请求一直以 Str 的形式出现,我怎样才能让它作为字典通过?
- sql-server - T-SQL - 查找具有时间间隔的记录
- hyperlink - 在活动链接上禁用悬停
- ios - DUXPreFlightStatusWidget 和 DUXPreflightChecklistController 之间的交互没有像宣传的那样工作
- python-3.x - 同一地址的异步共享对象不包含相同的值