Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Ug8lT-0001AE-Ex for pgsql-sql@arkaria.postgresql.org; Sat, 25 May 2013 07:19:43 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1Ug8lS-00020E-W9 for pgsql-sql@arkaria.postgresql.org; Sat, 25 May 2013 07:19:43 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Ug8lR-0001zc-3m for pgsql-sql@postgresql.org; Sat, 25 May 2013 07:19:41 +0000 Received: from mail.dhs-club.com ([208.69.229.2]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Ug8lI-0003TU-JI for pgsql-sql@postgresql.org; Sat, 25 May 2013 07:19:40 +0000 Received: from [192.168.200.149] (c-68-56-244-59.hsd1.fl.comcast.net [68.56.244.59]) (authenticated bits=0) by mail.dhs-club.com (8.14.4/8.14.4) with ESMTP id r4P7JTYg024419 (version=TLSv1/SSLv3 cipher=DHE-RSA-CAMELLIA256-SHA bits=256 verify=NO) for ; Sat, 25 May 2013 07:19:30 GMT Message-ID: <51A0660A.2010806@dhs-club.com> Date: Sat, 25 May 2013 03:19:38 -0400 From: Bill MacArthur User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130509 Thunderbird/17.0.6 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: reduce many loosely related rows down to one Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.70 on 208.69.229.2 X-Pg-Spam-Score: -3.0 (---) 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 Here is a boiled down example of a scenario which I am having a bit of difficulty solving. This is a catchall table where all the rows are related to the "id" but are entered by different unrelated processes that do not necessarily have access to the other data bits. CREATE TABLE test ( id INTEGER, rspid INTEGER, nspid INTEGER, cid INTEGER, iac BOOLEAN, newp SMALLINT, oldp SMALLINT, ppv NUMERIC(7,2), tppv NUMERIC(7,2) ); INSERT INTO test (id, rspid, nspid, cid, iac) VALUES (1,2,3,4,TRUE); INSERT INTO test (id, rspid, nspid, newp) VALUES (1,2,3,100); INSERT INTO test (id, rspid, nspid, oldp) VALUES (1,2,3,200); INSERT INTO test (id, rspid, nspid, tppv) VALUES (1,2,3,4100); INSERT INTO test (id, rspid, nspid, tppv) VALUES (1,2,3,3100); INSERT INTO test (id, rspid, nspid, ppv) VALUES (1,2,3,-100); INSERT INTO test (id, rspid, nspid, ppv) VALUES (1,2,3,250); INSERT INTO test (id, rspid, nspid, cid) VALUES (2,7,8,4); -- raw data now looks like this: select * from test; id | rspid | nspid | cid | iac | newp | oldp | ppv | tppv ----+-------+-------+-----+-----+------+------+---------+--------- 1 | 2 | 3 | 4 | t | | | | 1 | 2 | 3 | | | 100 | | | 1 | 2 | 3 | | | | 200 | | 1 | 2 | 3 | | | | | | 4100.00 1 | 2 | 3 | | | | | | 3100.00 1 | 2 | 3 | | | | | -100.00 | 1 | 2 | 3 | | | | | 250.00 | 2 | 7 | 8 | 4 | | | | | (8 rows) -- I want this result (where ppv and tppv are summed and the other distinct values are boiled down into one row) -- I want to avoid writing explicit UNIONs that will break if, say the "cid" was entered as a discreet row from the row containing "iac" -- in this example "rspid" and "nspid" are always the same for a given ID, however they could possibly be absent for a given row as well id | rspid | nspid | cid | iac | newp | oldp | ppv | tppv ----+-------+-------+-----+-----+------+------+---------+--------- 1 | 2 | 3 | 4 | t | 100 | 200 | 150.00 | 7200.00 2 | 7 | 8 | 4 | | | | 0.00 | 0.00 I have experimented with doing the aggregates as a CTE and then joining that to various incarnations of DISTINCT and DISTINCT ON, but those do not do what I want. Trying to find the right combination of terms to get an answer from Google has been unfruitful. Any ideas? Thank you for your consideration. Bill MacArthur -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql