<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Forum PostgreSQL - crosstab - jasne i proste wytłumaczenie]]></title>
	<link rel="self" href="http://forum.postgresql.org.pl/extern.php?action=feed&amp;tid=1460&amp;type=atom"/>
	<updated>2013-02-18T20:30:32Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.postgresql.org.pl/viewtopic.php?id=1460</id>
		<entry>
			<title type="html"><![CDATA[Odp: crosstab - jasne i proste wytłumaczenie]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=4054#p4054"/>
			<content type="html"><![CDATA[poczytaj helpa do crosstaba, twój przykład powinien wyglądać w uproszczeniu mniej więcej tak
select * from crosstab(
$$
select 'a'::text as wiersz,20130131::integer as kolumna,1 as sum
UNION ALL
select 'a',20121231,2
UNION ALL
select 'a',20121130,3
UNION ALL
select 'a',20121031,4
UNION ALL
select 'b',20120930,5
$$
,'values(20130131), (20121231), (20121130), (20121031), (20120930)'
)
as tabela
(wiersz text,"20130131" integer, "20121231" integer, "20121130" integer, "20121031" integer, "20120930" integer); 

Proponuję byś pobawił się danymi, a zobaczysz, że crosstab ma sporo ograniczeń choć jak już wiesz o co chodzi to całkiem przydatne narzędzie]]></content>
			<author>
				<name><![CDATA[c_michal]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=627</uri>
			</author>
			<updated>2013-02-18T20:30:32Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=4054#p4054</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Odp: crosstab - jasne i proste wytłumaczenie]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=4024#p4024"/>
			<content type="html"><![CDATA[Witam,

Sam sobie pokombinowałem i napisałem takie zapytanie:

select * from crosstab(
'
select
situation_date::integer,
sum(saldo_pln) as saldo
from msr_1301 group by 1
UNION ALL
select
situation_date::integer,
sum(saldo_pln) as saldo
from msr_1212 group by 1
UNION ALL
select
situation_date::integer,
sum(saldo_pln) as saldo
from msr_1211 group by 1
UNION ALL
select
situation_date::integer,
sum(saldo_pln) as saldo
from msr_1210 group by 1
UNION ALL
select
situation_date::integer,
sum(saldo_pln) as saldo
from msr_1209 group by 1
',
$$
VALUES ('20130131', '20121231', '20121130', '20121031', '20120930')
$$
)
as tabela
("20130131" integer, "20121231" integer, "20121130" integer, "20121031" integer, "20120930" integer);

i zwraca błąd "provided "categories" SQL must return 1 column of at least one row"

Wie ktoś o co może chodzić ?]]></content>
			<author>
				<name><![CDATA[bogo126]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=1542</uri>
			</author>
			<updated>2013-02-18T14:03:47Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=4024#p4024</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Odp: crosstab - jasne i proste wytłumaczenie]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=3984#p3984"/>
			<content type="html"><![CDATA[Dzięki za odpowiedź - przykład zanalizowany, tylko zastanawiam się: po co w crosstabie jest ten element generate series ?]]></content>
			<author>
				<name><![CDATA[bogo126]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=1542</uri>
			</author>
			<updated>2013-02-18T11:52:41Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=3984#p3984</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Odp: crosstab - jasne i proste wytłumaczenie]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=3939#p3939"/>
			<content type="html"><![CDATA[Tu masz opisany praktycznie taki sam problem z przykładem i opisem [url=http://forum.postgresql.org.pl/viewtopic.php?id=1424]Forum PostgreSQL[/url]]]></content>
			<author>
				<name><![CDATA[c_michal]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=627</uri>
			</author>
			<updated>2013-02-18T10:14:14Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=3939#p3939</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[crosstab - jasne i proste wytłumaczenie]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=3908#p3908"/>
			<content type="html"><![CDATA[Cześć Wam,

Jestem nowy na tym forum. Od około roku pracuję na postgresie - już wiele się nauczyłem, ale (jak widać) jeszcze wiele przede mną. Chciałbym Was zapytać o crosstaba - a mianowicie, aby ktoś mi go wytłumaczył tak od podstaw. Tak - przeczytałem dokumentację na stronce i temat o "drzewach" na forum. 

Załóżmy, że mam zapytanie, które daje wynik:
data_stanu ; suma_salda
20130131;  15
20121231;  16
20121130;  17
20121031;  18
20120930;  19

i chciałbym używajć crosstaba (bez crosstaba oczywiście umiem to zrobić) i otrzymać wynik w postaci:

20120930 ; 20121031 ; 20121130 ; 20121231 ; 20130131
19             ; 18             ; 17             ;16              ; 15

napisałem coś takiego:

select * from crosstab('zapytanie generujące dane wyjściowe',
$$values ('20120930'), ('20121031'), ('20121130'), ('20121231'), ('20130131')$$)
as tabela()

i nie wiem, co dalej. Może zamiast wymieniać daty, powinienem użyć generate series (1,5). Sam ni wiem. Proszę o pomoc. 

Pozdrawiam,
bogo126]]></content>
			<author>
				<name><![CDATA[bogo126]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=1542</uri>
			</author>
			<updated>2013-02-18T09:03:01Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=3908#p3908</id>
		</entry>
</feed>
