Received: from localhost (unknown [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 8FC5137C705 for ; Mon, 12 Nov 2007 16:27:58 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 76040-01-3 for ; Mon, 12 Nov 2007 16:27:54 -0400 (AST) Received: from ns1.officenet.no (vpn.officenet.no [193.212.174.13]) by postgresql.org (Postfix) with ESMTP id 8CCE737C6F4 for ; Mon, 12 Nov 2007 16:27:54 -0400 (AST) Received: from [89.10.27.2] (helo=andreak-laptop.local) by ns1.officenet.no with esmtpsa (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.63) (envelope-from ) id 1Irfsk-0005D3-Rn for pgsql-sql@postgresql.org; Mon, 12 Nov 2007 21:27:50 +0100 From: Andreas Joseph Krogh Organization: OfficeNet AS To: pgsql-sql@postgresql.org Subject: Problem with UNION-queries Date: Mon, 12 Nov 2007 21:27:14 +0100 User-Agent: KMail/1.9.6 (enterprise 0.20070907.709405) MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Message-Id: <200711122127.14860.andreak@officenet.no> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200711/55 X-Sequence-Number: 29583 Hi all I have a somewhat furry solution to a problem for which I think there might= be=20 a better way to do it. The table looks like this (simplified for the sake of this example): drop table if exists test1; create table test1( id serial primary key, key varchar, username varchar, value varchar ); =2D- Insert test-data insert into test1(username, key, value) values('andreak', 'A', 'value1'); insert into test1(username, key, value) values('andreak', 'A', 'value2'); insert into test1(username, key, value) values('andreak', 'A', 'value3'); insert into test1(username, key, value) values('andreak', 'B', 'value2'); insert into test1(username, key, value) values('andreak', 'B', 'value3'); insert into test1(username, key, value) values('andreak', 'B', 'value4'); insert into test1(username, key, value) values('andreak', null, 'value3'); insert into test1(username, key, value) values('andreak', null, 'value4'); insert into test1(username, key, value) values('andreak', null, 'value5'); =46or the sake of this example the test-case is greatly simplified, so I ha= ve=20 the following query to give me all rows for value=3D'A' and value=3D'B' select t.username, t.key, t.value from test1 t where t.key =3D 'A' UNION select t.username, t.key, t.value from test1 t where t.key =3D 'B' username | key | value =2D---------+-----+-------- andreak | A | value1 andreak | A | value2 andreak | A | value3 andreak | B | value2 andreak | B | value3 andreak | B | value4 (6 rows) Again, I know there are other, better, ways to accomplish that with this=20 simple schema, but again it's for the sake of the example. The important=20 thing here is that it's 2 UNION-queries providing the result. Now - what I'm trying to accomplish is getting the following result-set: username | key | value =2D---------+-----+-------- andreak | A | value1 andreak | A | value2 andreak | A | value3 andreak | B | value2 andreak | B | value3 andreak | B | value4 andreak | | value5 (7 rows) That is - I want all rows with username=3D'andreak' AND (key IS NULL) where= =20 the "value" is not in the previous result (not part of the other=20 UNION-queries). The hard, and important, part is that the resulting=20 rows' "value" must not exist in the "value"-column for any previous rows. Here is one way I figured out how to do it: select t.username, t.key, t.value from test1 t where t.key =3D 'A' UNION select t.username, t.key, t.value from test1 t where t.key =3D 'B' UNION select t.username, t.key, t.value from test1 t where t.value NOT IN ( select value from ( select t.username, t.key, t.value from test1 t where t.key =3D 'A' UNION select t.username, t.key, t.value from test1 t where t.key =3D 'B' ) tmp1 ) ; Given that my real schema is way more complex I'm looking for a solution wh= ich=20 doesn't involve issuing the NOT IN (original UNION-query) as it is a rather= =20 heavy query. Does anybody have a better approach to this problem? =2D-=20 Andreas Joseph Krogh Senior Software Developer / Manager =2D-----------------------+---------------------------------------------+ OfficeNet AS | The most difficult thing in the world is to | Karenslyst All=C3=A9 11 | know how to do a thing and to watch | PO. Box 529 Sk=C3=B8yen | somebody else doing it wrong, without | 0214 Oslo | comment. | NORWAY | | Tlf: +47 24 15 38 90 | | =46ax: +47 24 15 38 91 | | Mobile: +47 909 56 963 | | =2D-----------------------+---------------------------------------------+