• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 506
  • Last Modified:

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.
0
gogetsome
Asked:
gogetsome
  • 4
  • 2
1 Solution
 
SharathData EngineerCommented:
check this
Select contractitemnumber, itemnmbr, prclevel from bbtn..ct_itemcontractclin group by itemnmbr, prclevel
having count(contractitemnumber) > 1

Open in new window

0
 
chapmandewCommented:
almost...

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

0
 
gogetsomeAuthor Commented:
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.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
SharathData EngineerCommented:

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.
0
 
SharathData EngineerCommented:

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

Open in new window

0
 
chapmandewCommented:
check my first comment....
0
 
SharathData EngineerCommented:
forgot to include COUNT
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now