Link to home
Start Free TrialLog in
Avatar of gogetsome
gogetsomeFlag for United States of America

asked on

Aggregate in Where clause

Hello, how can I do something like this:

Select contractitemnumber, itemnmbr, prclevel from bbtn..ct_itemcontractclin
where count(contractitemnumber) > 1

It thows this error:
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
Avatar of Sharath S
Sharath S
Flag of United States of America image

check this
Select contractitemnumber, itemnmbr, prclevel from bbtn..ct_itemcontractclin group by itemnmbr, prclevel
having count(contractitemnumber) > 1

Open in new window

almost...

Select contractitemnumber, itemnmbr, prclevel from bbtn..ct_itemcontractclin group by contractitemnumber, itemnmbr, prclevel
having count(contractitemnumber) > 1

Avatar of gogetsome

ASKER

Sharath, it throws this:

Column 'bbtn..ct_itemcontractclin.contractitemnumber' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

You used count function. that means you are grouping the records in your table. So you have to include GROUP BY clause.
After that you want only the records having count(contractitemnumber) > 1. To include this filter, you should use HAVING clause as you are applying the filter after GROUPing records.
ASKER CERTIFIED SOLUTION
Avatar of Sharath S
Sharath S
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
check my first comment....
forgot to include COUNT