3

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_start_date DESC;

The problem is that I'm using a subquery in the FROM clause (also known as a "inline-view").

I've tried using .extra(tables=['...']) but Django adds quotes to the statement, making the SQL invalid.

I'd rather not resort to a .raw query. Is there a way to do this? Maybe through a reusable app if the core API doesn't provide a way.

EDIT:

This is the (seemingly) equivalent statement using a join:

    SELECT
    v.id, v.code, v.min_salary, v.max_salary, v.min_weekly_hours, v.max_weekly_hours, v.application_email, v.application_url, v.available_positions,
    MIN(CASE WHEN (p.active = True AND p.site_id = 1 AND p.start_date <= CURRENT_TIMESTAMP) THEN p.start_date ELSE NULL END) AS start_date, 
    MAX(CASE WHEN (p.active = True AND p.site_id = 1) THEN p.end_date ELSE NULL END) AS end_date
FROM base_vacancy v 
LEFT OUTER JOIN 
    base_vacancypublication p ON v.id = p.vacancy_id
WHERE v.workflow_status = 'A'
GROUP BY v.id, v.code, v.min_salary, v.max_salary, v.min_weekly_hours, v.max_weekly_hours, v.application_email, v.application_url, v.available_positions
HAVING MIN(CASE WHEN (p.active = True AND p.site_id = 1 AND p.start_date <= CURRENT_TIMESTAMP) THEN p.start_date ELSE NULL END) IS NOT NULL 
ORDER BY start_date DESC;

It's about ~3 times as slow, but it's possible to write this using Django 1.9 ORM methods:

Vacancy.objects.annotate(
    start_date=Min(
        Case(
            When(publication_set__is_active=True, publication_set__site_id=1, publication_set__start_date__lte=Now(), then='publication_set__start_date'),
            default=None
        )
    ),
    end_date=Max(
        Case(
            When(publication_set__is_active=True, publication_set__site_id=1, then='publication_set__end_date'),
            default=None
        )
    )
).filter(
    start_date__isnull=False, status=Workflow.APPROVED
).order_by(
    '-start_date'
)
jaap3
  • 2,696
  • 19
  • 34
  • Did you try to use a Join between those tables? – Kenzo_Gilead Jul 28 '16 at 10:11
  • I sure did, but this is not expressed easily as a join. There are multiple publications per vacancy and the aggregations are conditional. I did come to a (seemingly) equivalent statement, but it's about ~3 times as slow. – jaap3 Jul 28 '16 at 10:17
  • I can´t do it for you just now. I´ll check it out after. Anyway, you could try to launch a raw query against Postgres.... – Kenzo_Gilead Jul 28 '16 at 10:22
  • I am stuck in a similar issue.. di dyou find any solution to this ? – Mohan Apr 16 '20 at 11:09

0 Answers0