[SalesForce] How to combine 2 queries in to 1 soql

I have two queries that work separately but I cant find a way to combine the two.

Select company, convertedaccount.CreatedDate, CreatedDate From lead Where convertedaccountid In (select id from account) 

Select CreatedDate From AccountHistory Where Field = 'Funded__c'

The idea is to return lead created date, account created date, company name, and date when custom field Funded(that is in account) was modified.

Best Answer

You can't do this in SOQL.

SOQL (still!) does not support SQL-style arbitrary join queries.

Sub-queries in SOQL are limited to relationship queries (documentation here). Because you want data from the Lead table and the AccountHistory table, which have no native relationship, you cannot get both of them in one SOQL query.

You would need to do this either via Apex code (where you would take the two queries and combine their results using Apex), or by exporting the data and using a SQL database like Access/SQL Server/Oracle etc to query it.

Related Topic