<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Forum PostgreSQL - sumowanie kolumny]]></title>
	<link rel="self" href="http://forum.postgresql.org.pl/extern.php?action=feed&amp;tid=1900&amp;type=atom"/>
	<updated>2013-11-09T09:03:48Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.postgresql.org.pl/viewtopic.php?id=1900</id>
		<entry>
			<title type="html"><![CDATA[Odp: sumowanie kolumny]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=4800#p4800"/>
			<content type="html"><![CDATA[[quote=c_michal]Proszę (bez obsługi błędów i tylko jako przykład lub dla bardzo małej bazy danych)

CREATE TABLE public.tabelka
(  id serial primary key
  ,a int  
  ,b int
)
WITH (OIDS = FALSE);

CREATE or replace FUNCTION f_tabelka_tr() RETURNS trigger AS
$BODY$begin
if tg_op='INSERT' then
      if coalesce((select sum(a) from public.tabelka)+new.a,new.a,0) <=20 then
         return new;
      else
         return old;
      end if;
else
      if coalesce((select sum(a) from public.tabelka)+new.a-coalesce(old.a,0),new.a) <=20 then
         return new;
      else
         return old;
      end if;
end if;
return null;
end;$BODY$
LANGUAGE plpgsql VOLATILE NOT LEAKPROOF;

CREATE TRIGGER tr_tabelka BEFORE INSERT OR UPDATE ON tabelka FOR EACH ROW EXECUTE PROCEDURE public.f_tabelka_tr();[/quote]
////////////////////////////////////////////////////////////////

SUPER DZIĘKI !!
Jesteś mistrzem.
Zapraszam  [url]http://facebook.com/jacek.fleszar[/url]]]></content>
			<author>
				<name><![CDATA[fleszar]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=1665</uri>
			</author>
			<updated>2013-11-09T09:03:48Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=4800#p4800</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Odp: sumowanie kolumny]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=4798#p4798"/>
			<content type="html"><![CDATA[Proszę (bez obsługi błędów i tylko jako przykład lub dla bardzo małej bazy danych)

CREATE TABLE public.tabelka
(  id serial primary key
  ,a int  
  ,b int
)
WITH (OIDS = FALSE);

CREATE or replace FUNCTION f_tabelka_tr() RETURNS trigger AS
$BODY$begin
if tg_op='INSERT' then
      if coalesce((select sum(a) from public.tabelka)+new.a,new.a,0) <=20 then
         return new;
      else
         return old;
      end if;
else
      if coalesce((select sum(a) from public.tabelka)+new.a-coalesce(old.a,0),new.a,0) <=20 then
         return new;
      else
         return old;
      end if;
end if;
return null;
end;$BODY$
LANGUAGE plpgsql VOLATILE NOT LEAKPROOF;

CREATE TRIGGER tr_tabelka BEFORE INSERT OR UPDATE ON tabelka FOR EACH ROW EXECUTE PROCEDURE public.f_tabelka_tr();

Poprawiłem jeszcze jeden błąd]]></content>
			<author>
				<name><![CDATA[c_michal]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=627</uri>
			</author>
			<updated>2013-11-08T14:49:52Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=4798#p4798</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Odp: sumowanie kolumny]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=4797#p4797"/>
			<content type="html"><![CDATA[[quote=c_michal]Czy na pewno dobrze Cię rozumiem chcesz by przy update trigger sprawdzał, że użytkownik wprowadza wartość 20 i tylko na tą wartość ma pozwalać bo w przeciwnym przypadku ma zwracać wartość poprzednią (do tego dla wartości null w polu "a" trigger ma nic nie robić)? 

Jeśli chciałeś zrobić to tak że pole "a" nie może przyjmować wartości > niż 20 wystarczy zmodyfikować pole tabeli - skrypt tworzący przykładową tabelę
CREATE TABLE public.tabelka
(  id serial primary key
  ,a integer NOT NULL CHECK (a<=20)
) 
WITH (OIDS = FALSE);[/quote]
----------------------------------------------------
dzięki,ale chodzi raczej o to by suma kolumny (a) nie przekroczyła 20;
tabelka prosta :
a int,
b int;


BEGIN

if ( new.a <=20)  then
return new;
else
return old;
end if;
return null;
end;]]></content>
			<author>
				<name><![CDATA[fleszar]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=1665</uri>
			</author>
			<updated>2013-11-08T14:06:17Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=4797#p4797</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Odp: sumowanie kolumny]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=4780#p4780"/>
			<content type="html"><![CDATA[Czy na pewno dobrze Cię rozumiem chcesz by przy update trigger sprawdzał, że użytkownik wprowadza wartość 20 i tylko na tą wartość ma pozwalać bo w przeciwnym przypadku ma zwracać wartość poprzednią (do tego dla wartości null w polu "a" trigger ma nic nie robić)? 

Jeśli chciałeś zrobić to tak że pole "a" nie może przyjmować wartości > niż 20 wystarczy zmodyfikować pole tabeli - skrypt tworzący przykładową tabelę
CREATE TABLE public.tabelka
(  id serial primary key
  ,a integer NOT NULL CHECK (a<=20)
) 
WITH (OIDS = FALSE);]]></content>
			<author>
				<name><![CDATA[c_michal]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=627</uri>
			</author>
			<updated>2013-10-25T12:58:41Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=4780#p4780</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[sumowanie kolumny]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=4778#p4778"/>
			<content type="html"><![CDATA[Chcę postawić warunek liczby max. w kolumnie.


BEGIN


 if ( new.a ==20)  then
return new;
else
return old;
end if;
return null;
end;]]></content>
			<author>
				<name><![CDATA[fleszar]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=1665</uri>
			</author>
			<updated>2013-10-25T12:17:46Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=4778#p4778</id>
		</entry>
</feed>
