0

I have an custom Invoice object with a look-up relationship to Accounts.

I'm trying to query the data base to get the total number of invoices of the accounts where Connection_Date__c has a value (Connection_Date__c is a custom field of Accounts object)

How can I do this? The query I'm using gives me only the number of accounts but not the number of invoices.

SELECT Name,(SELECT name FROM Invoices__r) FROM Account WHERE Connection_Date__c != null

ivpavici
  • 1,117
  • 2
  • 19
  • 30
Chicho
  • 111
  • 1
  • 5

2 Answers2

1

In SOQL, it's almost easier to write queries that are driven from the child rather than the parent. This is opposite of SQL

Try a query that matches this pattern:

SELECT Count() FROM ChildTable WHERE ChildTable.parentField != Null

the_rj
  • 141
  • 6
-2
SELECT (Parent_Api_Name_In_Child_Object),
    COUNT(ID)
        (Child_Realtionship_Name__r.Parent_Fields....)
        FROM (Child_Object_Api_Name)
        GROUP BY (Parent_Api_Name_Child_Object,
                        Parent Feilds with API Names)
        HAVING COUNT(ID){>,<,=,{Optional}}

it is an SQL queried answer Let me know in case of any questions

Peter Tretyakov
  • 3,380
  • 6
  • 38
  • 54