>A questão é que eu ainda não entendi bem o seu problema. Talvez se
>você nos explicasse mais detalhes quem sabe poderíamos entender melhor
>e indicar melhores caminhos para seguir de forma a tirar um bom
>proveito dos recursos paquidérmicos de nosso elefante.

Minha estrutura

CREATE TABLE nota
(
  codigo integer NOT NULL DEFAULT 0,
  matricula character varying(50) NOT NULL,
  escola character varying(15) NOT NULL,
  curso character varying(50) NOT NULL,
  serie character varying(50) NOT NULL,
  turma character varying(50) NOT NULL,
  ano integer NOT NULL DEFAULT 0,
  disciplina integer NOT NULL DEFAULT 0,
  notac1 character varying(1) DEFAULT NULL::character varying,
  notan1 numeric(12,2) DEFAULT 0,
  falta1 integer DEFAULT 0,
  aulasdadas1 integer DEFAULT 0,
  dispensa1 character varying(3) DEFAULT NULL::character varying,
  notac2 character varying(1) DEFAULT NULL::character varying,
  notan2 numeric(12,2) DEFAULT 0,
  falta2 integer DEFAULT 0,
  aulasdadas2 integer DEFAULT 0,
  dispensa2 character varying(3) DEFAULT NULL::character varying,
  notac3 character varying(1) DEFAULT NULL::character varying,
  notan3 numeric(12,2) DEFAULT 0,
  falta3 integer DEFAULT 0,
  aulasdadas3 integer DEFAULT 0,
  dispensa3 character varying(3) DEFAULT NULL::character varying,
  notac4 character varying(1) DEFAULT NULL::character varying,
  notan4 numeric(12,2) DEFAULT 0,
  falta4 integer DEFAULT 0,
  aulasdadas4 integer DEFAULT 0,
  dispensa4 character varying(3) DEFAULT NULL::character varying,
  ca integer DEFAULT 0,
  cf character varying(50) DEFAULT NULL::character varying,
  cfn numeric(12,2) DEFAULT 0,
  dispensa_final character varying(3) DEFAULT NULL::character varying,
  resfinal character varying(50) DEFAULT NULL::character varying,
  dt_cad timestamp without time zone,
  usuario_cad character varying(50) DEFAULT NULL::character varying,
  dt_atualiza timestamp without time zone,
  usuario character varying(50) DEFAULT NULL::character varying,
  ca1 integer DEFAULT 0,
  ca2 integer DEFAULT 0,
  ca3 integer DEFAULT 0,
  ca4 integer DEFAULT 0,
  ma numeric(12,2),
  np numeric(12,2),
  pcon numeric(12,2) DEFAULT 0,
  rec numeric(12,2) DEFAULT 0,
  scon numeric(12,2) DEFAULT 0,
  CONSTRAINT nota_pkey PRIMARY KEY (codigo , matricula , escola , curso ,
serie , turma , ano , disciplina ),
  CONSTRAINT nota_codigo_fkey FOREIGN KEY (codigo, matricula, escola, curso,
serie, turma, ano)
      REFERENCES matricula (codigo, rm, escola, curso, serie, turma, ano)
MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT nota_disciplina_fkey FOREIGN KEY (disciplina, escola, curso,
serie, turma, ano)
      REFERENCES grade (disciplina, escola, curso, serie, turma, ano) MATCH
SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE
)
WITH (
  OIDS=FALSE
);
ALTER TABLE nota
  OWNER TO postgres;



CREATE TABLE matricula
(
  codigo integer NOT NULL DEFAULT 0,
  rm character varying(50) NOT NULL,
  escola character varying(15) NOT NULL,
  curso character varying(50) NOT NULL,
  serie character varying(50) NOT NULL,
  turma character varying(50) NOT NULL,
  ano integer NOT NULL DEFAULT 0,
  aluno character varying(50) DEFAULT NULL::character varying,
  naluno integer DEFAULT 0,
  jaestudou character varying(5) DEFAULT NULL::character varying,
  dt_situacao timestamp without time zone,
  situacao character varying(50) DEFAULT NULL::character varying,
  dt_cad timestamp without time zone,
  usuario_cad character varying(50) DEFAULT NULL::character varying,
  dt_atualiza timestamp without time zone,
  usuario character varying(50) DEFAULT NULL::character varying,

--estes campos daqui em diante, são somente para gráficos, estatísticas de
---cada matricula
  ad1 integer,
  f1 integer,
  ca1 integer,
  fr1 numeric(12,2) DEFAULT NULL::numeric,
  pf1 numeric(12,2) DEFAULT NULL::numeric,
  ac1 integer,
  cs1 numeric(12,2) DEFAULT NULL::numeric,
  media1 numeric(12,2) DEFAULT NULL::numeric,
  ad2 integer,
  f2 integer,
  ca2 integer,
  fr2 numeric(12,2) DEFAULT NULL::numeric,
  pf2 numeric(12,2) DEFAULT NULL::numeric,
  ac2 integer,
  cs2 numeric(12,2) DEFAULT NULL::numeric,
  media2 numeric(12,2) DEFAULT NULL::numeric,
  ad3 integer,
  f3 integer,
  ca3 integer,
  fr3 numeric(12,2) DEFAULT NULL::numeric,
  pf3 numeric(12,2) DEFAULT NULL::numeric,
  ac3 integer,
  cs3 numeric(12,2) DEFAULT NULL::numeric,
  media3 numeric(12,2) DEFAULT NULL::numeric,
  ad4 integer,
  f4 integer,
  ca4 integer,
  fr4 numeric(12,2) DEFAULT NULL::numeric,
  pf4 numeric(12,2) DEFAULT NULL::numeric,
  ac4 integer,
  cs4 numeric(12,2) DEFAULT NULL::numeric,
  media4 numeric(12,2) DEFAULT NULL::numeric,
  ad integer,
  f integer,
  ca integer,
  fr numeric(12,2) DEFAULT NULL::numeric,
  pf numeric(12,2) DEFAULT NULL::numeric,
  ac integer,
  cs numeric(12,2) DEFAULT NULL::numeric,
  media numeric(12,2) DEFAULT NULL::numeric,

  CONSTRAINT matricula_pkey PRIMARY KEY (codigo , rm , escola , curso ,
serie , turma , ano ),
  CONSTRAINT matricula_aluno_fkey FOREIGN KEY (aluno)
      REFERENCES aluno (raaluno) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT matricula_escola_fkey FOREIGN KEY (escola)
      REFERENCES escola (cie) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT matricula_turma FOREIGN KEY (escola, curso, serie, turma, ano)
      REFERENCES turma (escola, curso, serie, turma, ano) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE
)
WITH (
  OIDS=FALSE
);
ALTER TABLE matricula
  OWNER TO postgres;


Esta matricula é um exemplo, tenho estatísticas por disciplina, que seria
uma tabela grade, por turma, na tabela turma... hoje, eu executo da
aplicação periódicamente uma atualização nestes campos, minha idéia é tirar
da aplicação e trazer para o banco...

Usando uma função que diparada por uma trigger na tabela nota, atualizasse
os campos nas outras tabelas, 

Exemplo, o aluno tem 4 notas no ano, estamos ainda no 1 bimestre, eu preciso
testar se já tem aulas dadas no bimestre, somar as notas dos bimestres que
tem aulas dadas lançadas, e dividir pelo numero de bimestres... dando a
media atual... isto vale para porcentagem de faltas, freqüência, alem de
gravar as medias por bimestre também...

Pensando em disciplina ai fica mais complexo ainda, tenho que somar todas as
notas da turma, daquela disciplina, para determinar a media dela

Espero ter sido claro, 

Quero usar SQL, porem quero tirar da aplicação, automatizar no banco, nem
que eu não use trigger, mas faça uma chamada única direto por SQL no banco

Obrigado pela paciência



_______________________________________________
pgbr-geral mailing list
[email protected]
https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral

Responder a