Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix error “aggregate functions are not allowed in WHERE”

Tags:

postgresql

How to fi this error

Err] ERROR: aggregate functions are not allowed in WHERE

this my query

select count(case daftar.daftar when 'sd' then 1 else null end) as sd,
 count(case daftar.daftar when 'smp' then 1 else null end) as smp,
 count(case daftar.daftar when 'sma' then 1 else null end) as sma
from daftar
join gelombang on  daftar.gel=gelombang.id
join ajaran on ajaran.id=gelombang.id_ajar
join tahun on tahun.id=ajaran.tahun
where daftar.status='terima' and daftar.pindahan='no' and tahun.id= max(tahun.id)
like image 557
faza Avatar asked Dec 22 '16 06:12

faza


People also ask

Why are aggregate functions not allowed in WHERE clause?

Aggregate functions are not allowed because the WHERE clause is used for filtering data before aggregation. So while WHERE isn't for aggregation, it has other uses. To filter data based on an aggregate function result, you must use the HAVING clause.

Why the following statement is erroneous SELECT Dept_name ID avg salary from Instructorgroup by Dept_name?

1 dept_name, ID, avg (salary) FROM instructor GROUP BY dept_name; This statement IS erroneous because a) Avg(salary) should not be selected b) Dept_id should not be used in group by clause c) Misplaced group by clause d) Group by clause is not valid in this query Answer: b Explanation: Any attribute that is not present ...

How do you apply the aggregate function to a group of sets?

Explanation: We apply the aggregate function to a group of sets of tuples using the group by clause. The group by clause must always be used whenever we are willing to apply the aggregate function to a group of sets of tuples.


3 Answers

You can use "HAVING" to tackle this:

HAVING tahun.id= max(tahun.id)

select count(case daftar.daftar when 'sd' then 1 else null end) as sd,
 count(case daftar.daftar when 'smp' then 1 else null end) as smp,
 count(case daftar.daftar when 'sma' then 1 else null end) as sma
from daftar
join gelombang on  daftar.gel=gelombang.id
join ajaran on ajaran.id=gelombang.id_ajar
join tahun on tahun.id=ajaran.tahun
where daftar.status='terima' and daftar.pindahan='no' 
HAVING tahun.id= max(tahun.id)
like image 140
allen Avatar answered Oct 19 '22 06:10

allen


One option is to use a subquery to calculate that max value:

select count(case daftar.daftar when 'sd' then 1 else null end) as sd,
       count(case daftar.daftar when 'smp' then 1 else null end) as smp,
       count(case daftar.daftar when 'sma' then 1 else null end) as sma
from daftar
inner join gelombang
    on daftar.gel = gelombang.id
inner join ajaran
    on ajaran.id = gelombang.id_ajar
inner join tahun
    on tahun.id = ajaran.tahun
where daftar.status = 'terima' and
      daftar.pindahan = 'no'   and
      tahun.id = (select max(id) from tahun)
like image 38
Tim Biegeleisen Avatar answered Oct 19 '22 05:10

Tim Biegeleisen


Aggregates functions we use only in SELECT block. You can use inner select for this case:where daftar.status='terima' and daftar.pindahan='no' and tahun.id=(select max(id) from tahun)

like image 3
Mr. Skip Avatar answered Oct 19 '22 05:10

Mr. Skip