0

I want to do a query for all my opportunities (or contacts or whatever) and then join in the account so I can get the name etc. I'm able to get it to work querying accounts and joining the opportunities, but not the other way around.

select Name, Id, (select Id, Name, CloseDate, StageName, Amount from Opportunities where ownerId = 'id') from Account

Is there a way to do the query on opportunities instead? I don't want to get all accounts and loop through to get the opportunity data. Should be a simple belongs to type relationship.

I've tried such queries as:

select Id, Name, CloseDate, StageName, Amount, (select Name, Id from Account) from Opportunity where ownerId = 'id'

But that gives me "Didn't understand relationship 'Account' in FROM part of query call."

Thanks!

Cocoa Nub
  • 489
  • 7
  • 20

1 Answers1

2

For child->parent relationships you just use a dotted path to the fields you want, e.g.

Select id, Amount, CloseDate, Account.Name from Opportunity
superfell
  • 18,780
  • 4
  • 59
  • 81
  • One thing to keep in mind is that for custom objects (For example `Person__c`) the name changes to `__r` when you do the dot operator. That is, if you have a reference to a person from an account and you want the person's `Name` you'd have to do `Person__r.Name` – Egor Nov 19 '15 at 17:03