<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Forum PostgreSQL - SUM in postgres]]></title>
		<link>https://forum.postgresql.org.pl/viewtopic.php?id=1207</link>
		<description><![CDATA[Najświeższe odpowiedzi w SUM in postgres.]]></description>
		<lastBuildDate>Tue, 31 Jan 2012 07:45:22 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Odp: SUM in postgres]]></title>
			<link>https://forum.postgresql.org.pl/viewtopic.php?pid=3392#p3392</link>
			<description><![CDATA[Dzięki za odp, teraz wszystko jasne]]></description>
			<author><![CDATA[dummy@example.com (Rafik83)]]></author>
			<pubDate>Tue, 31 Jan 2012 07:45:22 +0000</pubDate>
			<guid>https://forum.postgresql.org.pl/viewtopic.php?pid=3392#p3392</guid>
		</item>
		<item>
			<title><![CDATA[Odp: SUM in postgres]]></title>
			<link>https://forum.postgresql.org.pl/viewtopic.php?pid=3391#p3391</link>
			<description><![CDATA[Rozumiem, że chodzi Ci o (non-persistent) [i]computed_column_expression[/i] z SQL Server ? Cytując [url=http://msdn.microsoft.com/en-us/library/ms174979.aspx]MSDN[/url]:

[quote]Is an expression that defines the value of a computed column. A computed column is a virtual column that is not physically stored in the table, unless the column is marked PERSISTED. The column is computed from an expression that uses other columns in the same table.[/quote]

Mogłoby się wydawać, że najlepszym odpowiednikiem byłby Postgresowy DEFAULT, jednakże [url=http://www.postgresql.org/docs/current/static/sql-createtable.html]dokumentacja[/url] jasno precyzuje, że nie można umieszczać odwołań do innych kolumn w ramach tabeli:

[quote]The DEFAULT clause assigns a default data value for the column whose column definition it appears within. The value is any variable-free expression (subqueries and cross-references to other columns in the current table are not allowed). The data type of the default expression must match the data type of the column.[/quote]

Jeśli jednak dobrze wczytać się w cytat z MSDN, to z naciskiem na słowa [b]non-persistent[/b] oraz [b]computed[/b] można zaradzić tworząc odpowiedni widok np. table3_view:

[code]Drop Table If Exists table3;
Create Table table3 (
        pole_1 integer Not Null,
        pole_2 integer Not Null,
        pole_3 integer Not Null
);

Drop View If Exists table3_view;
Create View table3_view As
        Select pole_1, pole_2, pole_3,
                pole_1 + pole_2 + pole_3 As total
        From table3;

Insert Into table3 Values (1, 3, 5), (2, 4, 6), (10, 20, 30);
INSERT 0 3
Select * From table3_view ;
 pole_1 | pole_2 | pole_3 | total 
--------+--------+--------+-------
      1 |      3 |      5 |     9
      2 |      4 |      6 |    12
     10 |     20 |     30 |    60
(3 rows)[/code]

[b]Edit:[/b]

Rozwiązanie z klauzulą DEFAULT nie przystaje także z tego powodu, że wartość (domyślna, początkowa) w takiej kolumnie umieszczana jest tylko raz tj. przy wstawaniu danych, a o ile dobrze rozumiem wartość kolumny wyliczeniowej z SQL Server jest [b]dynamiczna[/b] (analogicznie jak np. formuły w Excel'u), czyli tym bardziej właściwym odpowiednikiem jest widok. Szczerze powiedziawszy nie wiem jakie Postgres stosuje optymalizacje w tym zakresie i czy możliwe jest np. fizyczne przechowywanie aktualnie wyliczonych sum, choć też nie wiem czy w Twoim zastosowaniu byłoby to potrzebne.

[b]Edit 2:[/b]

Jeżeli potrzebujesz widok wraz z fizycznym przechowaniem danych bez używania trigger'ów (czyli odpowiednik dla [i]persistent computed_column_expression[/i]), to obecnie jest to work in progress i znajduje się na liście Todo tzn.:

[url]http://wiki.postgresql.org/wiki/Materialized_Views[/url]
[url]http://wiki.postgresql.org/wiki/Todo#Views_.2F_Rules[/url]

[quote]Right now materialized views require the user to create triggers on the main table to keep the summary table current. SQL syntax should be able to manage the triggers and summary table automatically. A more sophisticated implementation would automatically retrieve from the summary table when the main table is referenced, if possible. See [url=http://wiki.postgresql.org/wiki/Materialized_Views]Materialized Views[/url] for implementation details [/quote]]]></description>
			<author><![CDATA[dummy@example.com (gszpetkowski)]]></author>
			<pubDate>Mon, 30 Jan 2012 18:33:52 +0000</pubDate>
			<guid>https://forum.postgresql.org.pl/viewtopic.php?pid=3391#p3391</guid>
		</item>
		<item>
			<title><![CDATA[SUM in postgres]]></title>
			<link>https://forum.postgresql.org.pl/viewtopic.php?pid=3390#p3390</link>
			<description><![CDATA[Witam

mam pytanie na temat domyślnego sumowania pól w tabeli.

Mam tabelę:

CREATE TABLE "Test".table3 (
  pole_1 INTEGER, 
  pole_2 INTEGER, 
  pole_3 INTEGER
) WITHOUT OIDS;


Robię insert 

insert into test.table3 (pole_1,pole_2) values (1,1)

I chciałbym aby w polu pole_3  pojawiła mi się wartość wyliczona, czyli suma pole_1 + pole_2

Można zrobić triggera ale to rozwiązanie na tą chwilę nie wchodzi w grę. W innych systemach jest tak że można zdefiniować tak pole_3 aby było polem wyliczeniowym. Jest to możliwe w postgresql?

Czyli chodzi mi o coś takiego:


Create Table t1 (
[ID] int iNOT NULL,
[Month] char(3) NOT NULL,
[YEAR] smallint NOT NULL,
[ProdA] int NOT NULL,
[ProdB] int NOT NULL,
[ProdC] int NOT NULL,
[Total] as (ProdA + ProdB + ProdC)
);]]></description>
			<author><![CDATA[dummy@example.com (Rafik83)]]></author>
			<pubDate>Mon, 30 Jan 2012 13:12:17 +0000</pubDate>
			<guid>https://forum.postgresql.org.pl/viewtopic.php?pid=3390#p3390</guid>
		</item>
	</channel>
</rss>
