Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YYdKL-0005f8-4h for pgsql-sql@arkaria.postgresql.org; Thu, 19 Mar 2015 16:29:45 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YYdKK-0005u7-JU for pgsql-sql@arkaria.postgresql.org; Thu, 19 Mar 2015 16:29:44 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YYdKJ-0005tu-Cg for pgsql-sql@postgresql.org; Thu, 19 Mar 2015 16:29:43 +0000 Received: from mail-wi0-x232.google.com ([2a00:1450:400c:c05::232]) by magus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1YYdKF-00076p-Cl for pgsql-sql@postgresql.org; Thu, 19 Mar 2015 16:29:42 +0000 Received: by wixw10 with SMTP id w10so12580049wix.0 for ; Thu, 19 Mar 2015 09:29:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:disposition-notification-to:date:from:user-agent :mime-version:to:subject:content-type:content-transfer-encoding; bh=c2OVDxisdn4ZQdqHSJkzh/QRf2qzJ5VIBmkpNcMuLTA=; b=0G5OvXqAD3Q43SKFXEP8XMMliFuEO1iXj7K3easCd5Xr+BMO9AAIvhXp1BfylXaa45 DfkrvYKXp5YtoxClTbBNzFJr+BMveX5DtQdMrAmmX1fKzbsvYL2VoIrH7nuwCnWoSMHD wYEloWNSNQDCr0iYbwmUOO0w7Hfi1FhzWuj8fRwL9nRUuVC2UMD20k9DpZqwvg9WmEOU aGuTV6hyMYg7sGkgvH10ZI4UJzPTc98D9n7y1uCw063MjK3Goylwm20s1caYE95TbUzp cuMF5Cj9ShqsEufu3ki0AKDHj+33RfYHtqAzcee+TVuk6IDYH9lKyzEd5GV5Jr462m3z 9S7w== X-Received: by 10.180.198.162 with SMTP id jd2mr17337425wic.21.1426782578586; Thu, 19 Mar 2015 09:29:38 -0700 (PDT) Received: from [192.168.1.221] (hf5.z1.infracom.it. [82.193.17.245]) by mx.google.com with ESMTPSA id nb4sm2602440wjc.20.2015.03.19.09.29.36 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Mar 2015 09:29:37 -0700 (PDT) Message-ID: <550AF9A7.2090404@gmail.com> Date: Thu, 19 Mar 2015 17:30:31 +0100 From: agharta User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 MIME-Version: 1.0 To: "pgsql-sql@postgresql.org" Subject: Detect which sum of cartesian product (+ any combination of n records in tables) exceeds a value Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.5 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org Hi all, I hope someone can helps me.... I have a problem detecting a sum of cartesian product of tables. ---------------------------- Test case: //CREATE TABLES create table t1 ( id serial, field_1 integer); create table t2 ( id serial, field_1 integer); create table t3 ( id serial, field_1 integer); create table t4 ( id serial, field_1 integer); //FILL TABLES insert into t1 (field_1) select cast(random()*10 as integer) from generate_series(1,10); insert into t2 (field_1) select cast(random()*10 as integer) from generate_series(1,10); insert into t3 (field_1) select cast(random()*10 as integer) from generate_series(1,10); insert into t4 (field_1) select cast(random()*10 as integer) from generate_series(1,10); -------------------- Example: i have 4 tables with fields, i would detect which combination of field_1 in any table exceed a value (eg. 35). Simple, ugly & slow but simple: select * from t1, t2,t3,t4 where t1.field_1 + t2.field_1 + t3.field_1 + t4.field_1 >35 It works. Now my question: i would determine which combination on field_1 of t1,t2,t3 plus a combination(any) of 2 records on field_1 of t4, exceeds a value (eg. 35) It should be something like t1.field_1 + t2.field_1 + t3.field_1 + ( any combination of 2 records of t4.field_1) > 35 Suppose i have these records in tables (field_1), for simple explain of my problem: t1 = 1 t2 = 5 t3 = 4 t4 = 1,3,4 the combination of 2 record on t4.field_1 should be: 1+5+4 + ( 1+3) 1+5+4 + ( 1+4) 1+5+4 + ( 3+1) 1+5+4 + ( 3+4) 1+5+4 + ( 4+1) 1+5+4 + ( 4+3) How to do it??? This is a static test case with a static (2 records) problem, in my production db it could be any combination (2,3,4,5+ records ) of field_1 of any table. Hope I was clear, Best regards and thanks in advance, Agharta -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql