Re: Conditional left join

From: Alban Hertroys <haramrae(at)gmail(dot)com>
To: Amitabh Kant <amitabhkant(at)gmail(dot)com>
Cc: PGSQL Mailing List <pgsql-general(at)postgresql(dot)org>
Subject: Re: Conditional left join
Date: 2011-12-01 12:27:09
Message-ID: CAF-3MvN3=gCbG=A+5MrTdfgJw8VZ-aChw-y-2C5UN-qxQHrYOA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 1 December 2011 13:16, Amitabh Kant <amitabhkant(at)gmail(dot)com> wrote:
> I am trying (through conditional left join?) to fetch all records of tbldata
> and the operator name from tbloperators who was operating the unit at event
> time. If no operator was present, it should return null.

I think you want something akin to:

SELECT *
FROM tbldata AS a
LEFT JOIN (tbloperatorschedule INNER JOIN tbloperator USING (operator_id)) AS b
ON (a.unit_id = b.unit_id AND
a.event_time BETWEEN b.schedule_start_time AND b.schedule_end_time
)

--
If you can't see the forest for the trees,
Cut the trees and you'll see there is no forest.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Samba 2011-12-01 12:35:35 Re: How to restore the table space tar files created by pg_basebackup?
Previous Message Amitabh Kant 2011-12-01 12:16:38 Conditional left join