X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id B89D59DCADA for ; Wed, 22 Mar 2006 14:54:31 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 16685-06 for ; Wed, 22 Mar 2006 14:54:31 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from isb.com.pl (isb.com.pl [212.160.150.140]) by postgresql.org (Postfix) with ESMTP id 872DD9DCAD1 for ; Wed, 22 Mar 2006 14:54:29 -0400 (AST) Received: from maciek (sembrz [192.168.1.50]) by isb.com.pl (Postfix) with ESMTP id 6DA5E57842 for ; Wed, 22 Mar 2006 19:51:06 +0100 (CET) Date: Wed, 22 Mar 2006 19:54:28 +0100 From: Maciej Piekielniak X-Mailer: The Bat! (v1.62r) Reply-To: Maciej Piekielniak X-Priority: 3 (Normal) Message-ID: <79173292.20060322195428@isb.com.pl> To: pgsql-sql@postgresql.org Subject: Re: How to optimize this query? In-Reply-To: <684362e10603221032t4b07f5edu@mail.gmail.com> References: <827987076.20060322181715@isb.com.pl> <684362e10603220958s2dac37cfp@mail.gmail.com> <1731392587.20060322191801@isb.com.pl> <684362e10603221027t354b079bh@mail.gmail.com> <684362e10603221032t4b07f5edu@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.102 required=5 tests=[AWL=0.102] X-Spam-Score: 0.102 X-Spam-Level: X-Archive-Number: 200603/360 X-Sequence-Number: 24706 Hello Markus, Sorry, I try this: SELECT towar.id_towar, towar.key2, towar.nazwa, 0 AS min, 0 AS max, towar.ilosc_jed, towar.ilosc_nom, towar.ilosc_paczkowa, dostawcy.id_dostawcy, jednostka_miary.jednostka, 0.0 AS ilosc_magazyn, foo.z_zamowien, towar.key1 FROM towar JOIN ( SELECT zlecenia_towar.id_towar, SUM(zlecenia_towar.ilosc * zlecenia_elementy.ilosc) as z_zamowien FROM zlecenia_towar, zlecenia_elementy, zlecenia WHERE zlecenia_towar.id_zlecenia_elementy = zlecenia_elementy.id_zlecenia_elementy AND zlecenia_elementy.id_zlecenia = zlecenia.id_zlecenia AND zlecenia.id_paczka IN (52,50,53) GROUP BY zlecenia_towar.id_towar ) AS foo ON (foo.id_towar = towar.id_towar) LEFT JOIN dostawcy ON (towar.id_dostawcy = dostawcy.id_dostawcy) LEFT JOIN jednostka_miary ON (towar.id_jednostka_miary = jednostka_miary.id_jednostka_miary) WHERE towar.policzalne = True GROUP BY towar.id_towar, towar.key2, towar.nazwa, towar.min1, towar.max1, towar.ilosc_jed, towar.ilosc_nom, dostawcy.id_dostawcy, jednostka_miary.jednostka, towar.ilosc_paczkowa, towar.key1, foo.z_zamowien ORDER BY id_dostawcy; -- Best regards, Maciej mailto:piechcio@isb.com.pl