Question

I want to insert a tuple in my leggeapprovata table, but this table takes the attributes from 4 other tables. One of these 4 tables is the votazione table, from which it takes 6 attributes. Two do not give me errors, but for the others 4 attributes, it gives me the same error, ie that you can not use the aggregation function max in the where clause.

To avoid that the selection gives me more lines, since in the vote there are more tuples with different dates, I want the tuple with the most recent date (MAX (votazione.data)), but it gives me the error I was talking about before:

ERROR: the aggregation functions are not allowed in GROUP BY LINE 17: 
... ect votazione.favorevoli from votazione group by (MAX(vota ... ^
********** Error **********
ERROR: aggregate functions are not allowed in GROUP BY SQL state: 42803

I also tried with where, case when, having, but I still receive an error message.

Insert/Select statement

Following is the statement I am trying to execute.

INSERT INTO leggeapprovata (titolo, 
     relatore,
     numerolegislatura,
     testo,
     votisi,
     votino,
     astenuti,
     datapromulgazione,
     promulgatada,
     numlegge,
     dataapprovazionecamera,
     dataapprovazionesenato) 
  SELECT ddl.titolo, 
     ddl.relatore,
     legislatura.numero,
     ddl.testo,
     (select votazione.favorevoli from votazione group by (MAX(votazione.data)) AND tipoassemblea='senato'),
     (select votazione.contrari  from votazione group by (MAX(votazione.data)) AND tipoassemblea='senato'),
     (select votazione.astenuti  from votazione group by (MAX(votazione.data)) AND tipoassemblea='senato'),
     promulgazione.data,
     promulgazione.presidente,
     (select votazione.codice  from votazione  group by (MAX(votazione.data)) AND tipoassemblea='senato'),
     (select MAX(votazione.data) from votazione,ddl where tipoassemblea='camera' and votazione.ddl=ddl.titolo),
     (select MAX(votazione.data) from votazione,ddl where tipoassemblea='senato' and votazione.ddl=ddl.titolo)
   FROM ddl, legislatura, votazione, promulgazione
   WHERE ddl.promulgato='si' and ddl.titolo=promulgazione.ddl and (promulgazione.data between legislatura.datainizio and legislatura.datafine);

Table Definition

CREATE TABLE public.leggeapprovata
(
  titolo character varying(1000) NOT NULL,
  numerolegislatura integer NOT NULL,
  testo text,
  votisi integer,
  votino integer,
  astenuti integer,
  datapromulgazione date,
  promulgatada character varying(100),
  relatore character varying(100)[] NOT NULL,
  dataapprovazionecamera date,
  dataapprovazionesenato date,
  numlegge character(10),
  CONSTRAINT leggeapprovatapkey PRIMARY KEY (titolo, numerolegislatura, relatore),
  CONSTRAINT leggeapprovatanumerolegislaturafkey FOREIGN KEY (numerolegislatura)
      REFERENCES public.legislatura (numero) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT leggeapprovatapromulgatadafkey FOREIGN KEY (promulgatada)
      REFERENCES public.presidentedellarepubblica (presidente) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT leggeapprovatatestofkey FOREIGN KEY (testo, relatore)
      REFERENCES public.ddl (testo, relatore) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT leggeapprovatatitolofkey FOREIGN KEY (titolo, datapromulgazione)
      REFERENCES public.promulgazione (ddl, data) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT leggeapprovatavotisifkey FOREIGN KEY (votisi, votino, astenuti, numlegge)
      REFERENCES public.votazione (favorevoli, contrari, astenuti, codice) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT leggeapprovatatitolonumerolegislaturatestorelatorenukey UNIQUE (titolo, numerolegislatura, testo, relatore, numlegge)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE public.leggeapprovata
  OWNER TO postgres;
GRANT ALL ON TABLE public.leggeapprovata TO public;
GRANT ALL ON TABLE public.leggeapprovata TO postgres WITH GRANT OPTION;

Trigger Definitons

-- Trigger: inserimentoinleggeapprovata on public.leggeapprovata

-- DROP TRIGGER inserimentoinleggeapprovata ON public.leggeapprovata;

CREATE TRIGGER inserimentoinleggeapprovata
  BEFORE INSERT
  ON public.leggeapprovata
  FOR EACH ROW
  WHEN ((pg_trigger_depth() = 0))
  EXECUTE PROCEDURE public.inserimentoinleggeapprovata();
ALTER TABLE public.leggeapprovata DISABLE TRIGGER inserimentoinleggeapprovata;
COMMENT ON TRIGGER inserimentoinleggeapprovata ON public.leggeapprovata IS 'when (pg_trigger_depth()=)livello di annidamento corrente dei trigger PostgreSQL (0 se non viene chiamato, direttamente o indirettamente, da un trigger)';



CREATE TRIGGER inserimentoinleggeapprovata
  BEFORE INSERT
  ON public.leggeapprovata
  FOR EACH ROW
  WHEN ((pg_trigger_depth() = 0))
  EXECUTE PROCEDURE public.inserimentoinleggeapprovata();
COMMENT ON TRIGGER inserimentoinleggeapprovata ON public.leggeapprovata IS 'when (pg_trigger_depth()=)livello di annidamento corrente dei trigger PostgreSQL (0 se non viene chiamato, direttamente o indirettamente, da un trigger)';

Function Definitions

-- Function: public.inserimentoinleggeapprovata()

-- DROP FUNCTION public.inserimentoinleggeapprovata();

CREATE OR REPLACE FUNCTION public.inserimentoinleggeapprovata()
  RETURNS trigger AS
$BODY$
begin
if exists (select 1 from leggeapprovata
where leggeapprovata.titolo = NEW.titolo)
   THEN 
        RAISE EXCEPTION '% già ESISTENTE IN leggeapprovata', NEW.titolo;
   ELSE  RETURN new;
   end if;
end;

$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 1000;
ALTER FUNCTION public.inserimentoinleggeapprovata()
  OWNER TO postgres;
GRANT EXECUTE ON FUNCTION public.inserimentoinleggeapprovata() TO postgres;
GRANT EXECUTE ON FUNCTION public.inserimentoinleggeapprovata() TO public;

I would appreciate any feedback regarding the error message.


Votazione Table Data

This is the first image of the votazione table:

enter image description here

This is the second image of the votazione table:

enter image description here

Was it helpful?

Solution

Depending on what you are trying to achieve, your sub-selects might be as simple as converting this:

select votazione.favorevoli 
    from votazione 
group by (MAX(votazione.data)) 
AND tipoassemblea='senato'

...to this:

select votazione.favorevoli 
    from votazione 
where votazione.data = (select MAX(votazione.data) from votazione where tipoassemblea='senato')
AND tipoassemblea='senato'

The above statement has been modified

This might produce a single data set for your query, but that depends on the data you have.

As the error message correctly points out, you can't have an aggregate function in the GROUP BY clause.

Valid statements would be:

select votazione.favorevoli 
    from votazione 
where votazione.data = (select MAX(votazione.data) from votazione where tipoassemblea='senato') 
AND tipoassemblea='senato'

The above statement has been modified

...as previously pointed out, or possibly:

select votazione.favorevoli, MAX(votazione.data) 
    from votazione 
where tipoassemblea='senato'
GROUP BY votazione.favorevoli

This would however produce two values.

Please have a look at the official documentation to see how Aggregate Functions (PostgreSQL Documentation) are used.

You might be looking for something like:

SELECT ddl.titolo, 
 ...
 (select votazione.favorevoli from votazione where votazione.data = (select MAX(votazione.data) from votazione where tipoassemblea='senato') AND tipoassemblea='senato'),
 (select votazione.contrari  from votazione where votazione.data = (select MAX(votazione.data) from votazione where tipoassemblea='senato') AND tipoassemblea='senato'),
 (select votazione.astenuti  from votazione where votazione.data = (select MAX(votazione.data) from votazione where tipoassemblea='senato') AND tipoassemblea='senato'),
 ...
 (select votazione.codice  from votazione  where votazione.data = (select MAX(votazione.data) from votazione where tipoassemblea='senato') AND tipoassemblea='senato'),
 ...
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top