Re: Advice on re-writing a SELECT query. - Mailing list pgsql-sql

From Marc Mamin
Subject Re: Advice on re-writing a SELECT query.
Date
Msg-id B6F6FD62F2624C4C9916AC0175D56D880CDF6154@jenmbs01.ad.intershop.net
Whole thread Raw
In response to Advice on re-writing a SELECT query.  (JORGE MALDONADO <jorgemal1960@gmail.com>)
List pgsql-sql
I have a query like this:
>
>SELECT
>lpt_titulo AS tmt_titulo,
>tmd_nombre AS tmt_nombre,
>tmd_album AS tmt_album
>SUM(lpt_puntos) AS tmt_puntos,
>lpt_fuente AS tmt_fuente
>FROM listas_pre_titulos, temp_lista_titulos
>WHERE
>listas_pre_titulos.lpt_tipo = 3 AND
>listas_pre_titulos.lpt_titulo <> temp_lista_titulos.tmt_titulo AND
>listas_pre_titulos.tmd_album <> temp_lista_titulos.tmt_album AND
>listas_pre_titulos.lpt_fuente <> temp_lista_titulos.tmt_fuente
>GROUP BY
>lpt_fuente, lpt_titulo, tmd_album
>ORDER BY tmt_puntos ASC
>
>Is it valid to re-write the FROM and WHERE statements as follows?
>
>FROM listas_pre_titulos
>INNER JOIN temp_lista_titulos ON
>(listas_pre_titulos.lpt_titulo, listas_pre_titulos.tmd_album, listas_pre_titulos.lpt_fuente)
>NOT IN
>(temp_lista_titulos.tmt_titulo, temp_lista_titulos.tmt_album, temp_lista_titulos.tmt_fuente)
>WHERE listas_pre_titulos.lpt_tipo = 3

hello,
your second syntax is not valid sql, but you can achieve it as in this example:

create temp table a(a int,b int,c int,d int);
create temp table b(a int,b int,c int,d int);

select * from a join b ON ((a.a,a.b,a.c)<>(b.a,b.b,b.c))

but beware if null values are involved( 1<>NULL => NULL).
In this case you can use :
select * from a join b ON ((a.a,a.b,a.c) IS DISTINCT FROM (b.a,b.b,b.c))

regards,

Marc Mamin

pgsql-sql by date:

Previous
From: JORGE MALDONADO
Date:
Subject: Advice on re-writing a SELECT query.
Next
From: Guillaume Lelarge
Date:
Subject: Re: DELETE...RETURNING problem with libpq