Обсуждение: crosstab doesn't work

Поиск
Список
Период
Сортировка

crosstab doesn't work

От
Vittorio
Дата:
Dear all,

I installed postgresql 8.04 on a pentium box with freebsd
5.4

# psql --version
psql (PostgreSQL) 8.0.4


In the db switch I have
the following ct1 table

switch=# select * from ct1 order by 1,2;
 id |
rowclass | rowid | attribute | value
----+----------+-------+-----------
+-------
  1 | group1   | test1 | att1      |     8
  2 | group1   |
test1 | att2      |    15
  3 | group1   | test1 | att3      |    -1

4 | group1   | test1 | att4      |    12
  5 | group1   | test2 |
att1      |    17
  6 | group1   | test2 | att2      |   456
  7 |
group1   | test2 | att3      |     2
  8 | group1   | test2 | att4
|   -12
  9 | group1   | test1 | att1      |    10
 10 | group1   |
test2 | att2      |  -420
(10 righe)

crosstable seems to work (even
though with wierd, wrong results for which I blame the repetition of
some records and values) :

select * from crosstab('select rowid,
attribute, value from ct1 where rowclass = ''group1'' order by 1,2;',
2) as c(rowid text, att1 integer, att2 integer);
 rowid | att1 | att2
-------+------+------
 test1 |    8 |   10
 test2 |   17 | -420
(2
righe)

If I aggregate

switch=# select rowid,attribute, sum(value) as
val from ct1 group by rowid, attribute order by 1,2;
 rowid | attribute
| val
-------+-----------+-----
 test1 | att1      |  18
 test1 |
att2      |  15
 test1 | att3      |  -1
 test1 | att4      |  12

test2 | att1      |  17
 test2 | att2      |  36
 test2 | att3      |
2
 test2 | att4      | -12
(8 righe)

crosstab doesn't seem to work
anymore putting the above select into crosstab

switch=# select * from
crosstab('select rowid,attribute, sum(value) as val from ct1 group by
rowid, attribute order by 1,2;', 2) as c(rowid text, att1 integer, att2
integer);
ERROR:  query-specified return row and actual function return
row do not match

Please help I'm in badly need of the crosstab
function.

Ciao
Vittorio


Re: crosstab doesn't work

От
Martijn van Oosterhout
Дата:
On Fri, Nov 25, 2005 at 03:09:06PM +0100, Vittorio wrote:
> Dear all,

<snip>

> crosstab doesn't seem to work
> anymore putting the above select into crosstab
>
> switch=# select * from
> crosstab('select rowid,attribute, sum(value) as val from ct1 group by
> rowid, attribute order by 1,2;', 2) as c(rowid text, att1 integer, att2
> integer);
> ERROR:  query-specified return row and actual function return
> row do not match

It's just a guess, but I think the result of sum() might be a bigint,
in which case you should say that att1 and att2 are bigints, or
alternativly use cast(sum(value) as integer).

Hope this helps,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.

Вложения