pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Maybe a bug with SQL processor
2+ messages / 2 participants
[nested] [flat]

* Maybe a bug with SQL processor
@ 1999-06-15 18:01 Charles <charles@psmi.com.br>
  1999-06-15 23:14 ` Re: [SQL] Maybe a bug with SQL processor Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 2+ messages in thread

From: Charles @ 1999-06-15 18:01 UTC (permalink / raw)
  To: 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




^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: [SQL] Maybe a bug with SQL processor
  1999-06-15 18:01 Maybe a bug with SQL processor Charles <charles@psmi.com.br>
@ 1999-06-15 23:14 ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 2+ messages in thread

From: Tom Lane @ 1999-06-15 23:14 UTC (permalink / raw)
  To: Charles <charles@psmi.com.br>; +Cc: pgsql-sql

Hint: this'd work a lot better if levelstr were declared varchar() or
text.  The padding spaces for char() are messing up your match pattern.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~1999-06-15 23:14 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1999-06-15 18:01 Maybe a bug with SQL processor Charles <charles@psmi.com.br>
1999-06-15 23:14 ` Tom Lane <tgl@sss.pgh.pa.us>

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