在 postgresql 中重用子查询

reusing subquery in postgresql

我有一个工作正常的查询,但是我在其中两次使用相同的子查询。我想知道是否有办法重用这个子查询。这是查询:

SELECT DISTINCT homeworks.*
FROM homeworks
INNER JOIN homework_messages ON homeworks.id = homework_messages.homework_id
WHERE homework_messages.message_type = 'submit'
  AND homework_messages.created_at::date <= (CURRENT_DATE - '5 days'::interval)
  AND (
    (
      SELECT MAX(homework_messages.created_at) FROM homework_messages
      WHERE homework_messages.homework_id = homeworks.id AND homework_messages.message_type != 'submit'
    ) < homework_messages.created_at 
    OR 
    (
      SELECT MAX(homework_messages.created_at) FROM homework_messages
      WHERE homework_messages.homework_id = homeworks.id AND homework_messages.message_type != 'submit'
    ) IS NULL
  )
GROUP BY homeworks.id

如您所见,我将这些行重复了两次:

(
  SELECT MAX(homework_messages.created_at) FROM homework_messages
  WHERE homework_messages.homework_id = homeworks.id AND homework_messages.message_type != 'submit'
)

我尝试使用 WITH 子句,但它似乎不适合这个问题,或者我用错了。无论如何,谢谢你的帮助。

这里有一个 cross join lateral 建议。我没有深入研究您的业务逻辑,只是试图使其保持等同。

SELECT DISTINCT homeworks.*
FROM homeworks
INNER JOIN homework_messages ON homeworks.id = homework_messages.homework_id
cross join lateral ( -- your subquery follows
  SELECT MAX(created_at) as created_at FROM homework_messages
  WHERE homework_id = homeworks.id AND message_type != 'submit'
) as lat
WHERE homework_messages.message_type = 'submit'
  AND homework_messages.created_at::date <= (CURRENT_DATE - '5 days'::interval)
  AND (lat.created_at < homework_messages.created_at OR lat.created_at IS NULL)
GROUP BY homeworks.id;