Received: from smtp.maringa.intranet (linha-032.col.psi.br [200.203.189.172]) by hub.org (8.9.3/8.9.3) with SMTP id OAA68865 for ; Tue, 15 Jun 1999 14:07:31 -0400 (EDT) (envelope-from charles@psmi.com.br) Received: (qmail 11213 invoked from network); 15 Jun 1999 15:08:21 -0000 Received: from unknown (HELO blumenau) (10.0.0.66) by 10.0.0.67 with SMTP; 15 Jun 1999 15:08:21 -0000 Message-Id: <3.0.6.32.19990615150150.00797960@10.0.0.67> X-Sender: charles@10.0.0.67 (Unverified) X-Mailer: QUALCOMM Windows Eudora Light Version 3.0.6 (32) Date: Tue, 15 Jun 1999 15:01:50 -0300 To: pgsql-sql@postgresql.org From: Charles Subject: Maybe a bug with SQL processor Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" It seems to be a problem with PostgreSQL 6.5 beta ... I have the following tables: CREATE TABLE levels ( levelstr char(40) PRIMARY KEY, title char(50) ); CREATE TABLE entries ( code int4 PRIMARY KEY, levelstr char(40), value float ); Tables' records are: INSERT INTO levels VALUES ('0001','level one'); INSERT INTO levels VALUES ('00010001','level one.one'); INSERT INTO levels VALUES ('00010002','level one.two'); INSERT INTO levels VALUES ('000100020001','level one.two.one'); INSERT INTO levels VALUES ('00010003','level one.three'); INSERT INTO levels VALUES ('0002','level two'); INSERT INTO levels VALUES ('00020001','level two.one'); INSERT INTO levels VALUES ('00020002','level two.two'); INSERT INTO levels VALUES ('0003','level three'); INSERT INTO entries VALUES (1,'000100020001',50.50); INSERT INTO entries VALUES (2,'000100020001',149.50); INSERT INTO entries VALUES (3,'00020002',100); INSERT INTO entries VALUES (4,'00020002',-99.99); INSERT INTO entries VALUES (5,'00010001',50); I have to compute the sum of each "value" (in "entries" table) which correspond to each "levelstr" (in "levels" table), Every "value" that sums to a "levelstr", also sums to your parent "levelstr" on query. For example, "000100020001" has "00010002" as your parent, which, by your turn, has the "0001" as your parent, then the entry values 50.50 plus 149.50 adds 200 to "000100020001", "00010002" and "0001". Thus, I expect the following query result: +--------------+-----------+ | levelstr | level_sum | +--------------+-----------+ | 0001 | 250 | | 00010001 | 50 | | 00010002 | 200 | | 000100020001 | 200 | | 00010003 | | | 0002 | 0,01 | | 00020001 | | | 00020002 | 0,01 | | 0003 | | +--------------+-----------+ To make this query I've tried... CREATE FUNCTION level_sum(bpchar) RETURNS float AS ' SELECT sum(value) FROM entries WHERE levelstr LIKE $1 || ''%'';' LANGUAGE 'sql'; SELECT levelstr, level_sum(levelstr) FROM levels; But the result set was: +--------------+-----------+ | levelstr | level_sum | +--------------+-----------+ | 0001 | | | 00010001 | 50 | | 00010002 | | | 000100020001 | 200 | | 00010003 | | | 0002 | | | 00020001 | | | 00020002 | 0,01 | | 0003 | | +--------------+-----------+ also look this... SELECT level_sum('0001'); +-----------+ | level_sum | +-----------+ | 250 | +-----------+--> level_sum is correctly evaluated! and... SELECT levelstr, level_sum( levelstr ) FROM levels WHERE levelstr='0001'; +--------------+-----------+ | levelstr | level_sum | +--------------+-----------+ | 0001 | | +--------------+-----------+--> level_sum is incorrect! WHAT IS WRONG? I've tried many other possibilities to take this query work, but I had no success! CAN YOU HELP ME? Thanks a lot! Charles Roberto Stempniak charles@psmi.com.br