+ Start a Discussion
Rohit TripathiRohit Tripathi 

How can we use result of sub query which uses Group By and Having in main query ?

Hi,

I am working on a query where I have to count distinct values from inside query in related object using Group By and Having and use that value in main query.

For example :

select Firstname from Contact where Account.Name in (
SELECT Name
FROM Account
GROUP BY Name
HAVING Count(Id) = 1)

This query is throwing error : "Unknown error parsing query"
Jithin Krishnan 2Jithin Krishnan 2
The query should be an SOQL query and not SQL. Please go through the below link:
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/
Rohit TripathiRohit Tripathi
I have mentioned SOQL query in my question. Count(ID) is substitute of Count(*) in SQL. I need the corrected query to solve this issue. 
Jithin Krishnan 2Jithin Krishnan 2
Sorry I missed that. You can use something like this:
select firstname, Account.name from Contact group by Account.name having count(Account.name)=1