Re: Advice on re-writing a SELECT query.

From: Marc Mamin <M(dot)Mamin(at)intershop(dot)de>
To: JORGE MALDONADO <jorgemal1960(at)gmail(dot)com>, "pgsql-sql(at)postgresql(dot)org" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Advice on re-writing a SELECT query.
Date: 2013-05-25 17:52:23
Message-ID: B6F6FD62F2624C4C9916AC0175D56D880CDF6154@jenmbs01.ad.intershop.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Guillaume Lelarge 2013-05-26 06:51:19 Re: DELETE...RETURNING problem with libpq
Previous Message JORGE MALDONADO 2013-05-25 17:17:20 Advice on re-writing a SELECT query.