left join conditon causes error

From: "Andrus" <kobruleht2(at)hot(dot)ee>
To: pgsql-general(at)postgresql(dot)org
Subject: left join conditon causes error
Date: 2008-10-08 20:35:50
Message-ID: gcj5jb$1usd$1@news.hub.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Query

SELECT
bilkaib.*
FROM prpalk,
(SELECT TRUE AS db, 1 AS sign UNION SELECT FALSE,-1 ) role,konto
destkonto,bilkaib
LEFT JOIN kurss sihrkurs ON CASE WHEN '00' IN(cr,db) THEN
bilkaib.kuupaev-1 ELSE bilkaib.kuupaev END
=sihrkurs.kuupaev AND sihrkurs.raha=destkonto.raha
WHERE CASE WHEN role.db THEN bilkaib.db ELSE bilkaib.cr
END=destkonto.kontonr
AND (destkonto.raha=prpalk.pohiraha OR sihrkurs.raha=destkonto.raha )

AND bilkaib.summa<>0 AND destkonto.kontonr<>'00'
AND (bilkaib.kuupaev='2008-01-01 00:00:00.000000' OR (bilkaib.cr!='00' AND
bilkaib.db!='00'))

AND destkonto.kontonr LIKE '111%' ESCAPE '!'
AND bilkaib.kuupaev BETWEEN '2008-01-01 00:00:00.000000' AND '2008-01-03
00:00:00.000000'

causes error

ERROR: invalid reference to FROM-clause entry for table "destkonto"
LINE 7: ... =sihrkurs.kuupaev AND sihrkurs.raha=destkonto....
^
HINT: There is an entry for table "destkonto", but it cannot be referenced
from this part of the query.

I tried to remove AND sihrkurs.raha=destkonto.raha

and move it to where clause:

AND (destkonto.raha=prpalk.pohiraha OR sihrkurs.raha=destkonto.raha )

from left join but in this case query returns duplicate rows from bilkaib
table.

Why destkonto table cannot used but other tables in FROM list can be used in
join condition ?
How to use such left join ?

Andrus.

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Chris Henderson 2008-10-09 03:29:30 review db dump script and difference between pg_dump & pg_dumpall
Previous Message Anton Andreev 2008-10-08 20:08:14 problem with check constraints