inline-view

How to get a subquery in FROM clause in Django ORM

对着背影说爱祢 提交于 2020-05-15 05:15:08
问题 I'm trying to express the following (Postgres) SQL statement using the Django ORM: SELECT v.id, v.min_salary, v.max_salary, v.min_weekly_hours, v.max_weekly_hours p.min_start_date, p.max_end_date FROM vacancy v, ( SELECT id, vacancy_id, MIN(start_date) min_start_date, MAX(end_date) AS max_end_date FROM vacancypublication WHERE (active = True AND site_id = 1 AND start_date <= CURRENT_TIMESTAMP) GROUP BY id, vacancy_id ) p WHERE p.vacancy_id = v.id AND v.workflow_status = 'A' ORDER BY p.min

SQL - Relationship between a SubQuery and an Outer Table

不打扰是莪最后的温柔 提交于 2019-11-30 22:38:26
问题 Problem I need to better understand the rules about when I can reference an outer table in a subquery and when (and why) that is an inappropriate request. I've discovered a duplication in an Oracle SQL query I'm trying to refactor but I'm running into issues when I try and turn my referenced table into a grouped subQuery. The following statement works appropriately: SELECT t1.* FROM table1 t1, INNER JOIN table2 t2 on t1.id = t2.id and t2.date = (SELECT max(date) FROM table2 WHERE id = t1.id)