patternsqlMinor
Issue with Aggregate Function
Viewed 0 times
withissuefunctionaggregate
Problem
I want to insert a tuple in my
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
I also tried with
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 wh
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: 42803I 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 wh
Solution
Depending on what you are trying to achieve, your sub-selects might be as simple as converting this:
...to this:
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
Valid statements would be:
The above statement has been modified
...as previously pointed out, or possibly:
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 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.favorevoliThis 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'),
...Code Snippets
select votazione.favorevoli
from votazione
group by (MAX(votazione.data))
AND tipoassemblea='senato'select votazione.favorevoli
from votazione
where votazione.data = (select MAX(votazione.data) from votazione where tipoassemblea='senato')
AND tipoassemblea='senato'select votazione.favorevoli
from votazione
where votazione.data = (select MAX(votazione.data) from votazione where tipoassemblea='senato')
AND tipoassemblea='senato'select votazione.favorevoli, MAX(votazione.data)
from votazione
where tipoassemblea='senato'
GROUP BY votazione.favorevoliSELECT 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'),
...Context
StackExchange Database Administrators Q#228545, answer score: 2
Revisions (0)
No revisions yet.