Maybe a bug with SQL processor - Mailing list pgsql-sql

From Charles
Subject Maybe a bug with SQL processor
Date
Msg-id 3.0.6.32.19990615150150.00797960@10.0.0.67
Whole thread Raw
Responses Re: [SQL] Maybe a bug with SQL processor
List pgsql-sql
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



pgsql-sql by date:

Previous
From: "Collin F. Lynch"
Date:
Subject: ...
Next
From: Brook Milligan
Date:
Subject: Re: [SQL] example of trigger to track DB changes