From: Charles <charles@psmi.com.br>
To: pgsql-sql@postgresql.org
Subject: Maybe a bug with SQL processor
Date: Tue, 15 Jun 1999 15:01:50 -0300
Message-ID: <3.0.6.32.19990615150150.00797960@10.0.0.67> (raw)
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-sql@postgresql.org
Cc: charles@psmi.com.br
Subject: Re: Maybe a bug with SQL processor
In-Reply-To: <3.0.6.32.19990615150150.00797960@10.0.0.67>
References: <3.0.6.32.19990615150150.00797960@10.0.0.67>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox