Re: RV: bad result in a query!! :-(

From: Jochem van Dieten <jochemd(at)oli(dot)tudelft(dot)nl>
To: Jose Antonio Leo <jaleo8(at)storelandia(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: RV: bad result in a query!! :-(
Date: 2002-10-17 14:14:21
Message-ID: 1034864061.3daec5bddd7e7@webmail.oli.tudelft.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Query rewritten with some indentation:

SELECT
vtdiaaec.cod_ae1,
aecoc.des_ae,

Sum(vtdiaaec.ven_uni) AS
-- You are missing something here, copy-paste error I presume

Sum(vtdiaaec.ven_pco) AS SumaDeven_pco,
Sum(vtdiaaec.ven_siv) AS SumaDeven_siv,
Sum(vtdiaaec.ven_civ) AS SumaDeven_civ,
Sum(vtdiaaec.ven_ofe) AS SumaDeven_ofe,
Sum(vtdiaaec.cos_ofe) AS SumaDecos_ofe
FROM
vtdiaaec LEFT JOIN aecoc ON vtdiaaec.cod_ae1 = aecoc.cod_ae1
WHERE
aecoc.cod_ae2=0
AND aecoc.cod_ae3=0
AND aecoc.cod_ae4=0
AND aecoc.cod_ae5=0
AND extract (year from vtdiaaec.fecha)='2002'
GROUP BY
vtdiaaec.cod_ae1,
aecoc.des_ae
ORDER BY
vtdiaaec.cod_ae1;

> The table aecoc has a primary key (cod_ae1,cod_ae2,cod_ae3) and the
> table vtdiaaec has a key with the fields cod_ae1,cod_ae2,cod_ae3.

Something I don't understand about this query: why the LEFT JOIN and
not an INNER JOIN? Isn't it true that each row of the vtdiaaec table
that does not have a matching row in aecoc table gets included in the
join result with each field that originates from the aecoc table set to
NULL?
But then, you later remove the rows anyway by adding predicates that
exclude all rows where these fields do not have a 0 value. Wouldn't you
get the same result by using an INNER JOIN instead of a LEFT JOIN? Or
am I missing something?

Jochem

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message James Hall 2002-10-17 14:21:54 Re: Pg_dumpall problem
Previous Message Fathi Ben Nasr 2002-10-17 14:05:48 Re: PostgreSQL Benchmarks