[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 496
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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