Salesforce REST API SOQL get account for opportunies - salesforce

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!

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

Related

Child to Parent SOQL Query

I need a query between Opportunity and OpportunityLineItem.
OpportunityLineItem has a lookup for Opportunity. Child relationship name is OpportunityLineItems. Opportunity Lookup field is called OpportunityId.
I'm trying the below query but it doesn't work.
select Id,Name,OpportunityId__r.Description from OpportunityLineItem
Also tried:
select Id,Name,OpportunityLineItems__r.Description from OpportunityLineItem
Top-down
SELECT Id, Name, Description,
(SELECT Id, Name FROM OpportunityLineItems)
FROM Opportunity
Bottom-up
SELECT Id, Name, Opportunity.Name, Opportunity.Description
FROM OpportunityLineItem
Check out the links in https://stackoverflow.com/a/73913839/313628 and https://stackoverflow.com/a/73877986/313628 too

GROUP BY Works with Select columns not in GROUP BY

why is this GROUP BY still working when the SELECTed columns are neither in the GROUP BY clause, nor aggregate function.
DATABASE SCHEMA HERE
SELECT FirstName,
LastName,
City,
Email,
COUNT(I.CustomerId) AS Invoices
FROM Customers C INNER JOIN Invoices I
ON C.CustomerId = I.CustomerId
GROUP BY C.CustomerId
This syntax is allowed and documented in SQLite: Bare columns in an aggregate query.
The columns FirstName, LastName, City, Email are called bare columns.
Such columns get an arbitrary value with the exception of the case where one (and only this one) of MIN() or MAX() is used. In this case the values of the bare columns are taken from the row that contains then min or max aggregated value.
In any case be careful when you use this syntax because you would get unexpected results.
Two things I want to talk about:
Group by will work if the column name exists in the table you are working on. In your query, you have inner join Customer table with Invoice table. From your schema, I can see in the Invoice table CustomerId column exists.
In SQL serve have to give all the column name that you selected plus your desired column name. What I mean by that your query should be like this.
SELECT FirstName,
LastName,
City,
Email,
COUNT(I.CustomerId) AS Invoices
FROM Customers C INNER JOIN Invoices I
ON C.CustomerId = I.CustomerId
GROUP BY C.CustomerId,
LastName,
City,
Email
So, I think you are using MySQL that's why it's working.

Get Account lifetime value (sum of opportunities) and details

I'm trying to get a table made of rows with Account details and the Sum of the Amount of the Opportunities for that Account.
SELECT Sum(Opportunity.Amount), Opportunity.AccountId FROM Opportunity GROUP BY AccountId
With this I get a simple list with the value I want and the AccountId.
Now I'd also like the Account details like PersonName.
How can I do that?
I've tried this:
SELECT Sum(Opportunity.Amount), Opportunity.AccountId, Account.PersonName
FROM Opportunity
GROUP BY Opportunity.AccountId
INNER JOIN Account WHERE Account.Id=AccountId
But I get Unknown error parsing query. Why?
I don't have Person Accounts enabled so you'll have to add your own fields but something like this should be good start.
SELECT AccountId, Account.Name, Account.Website, SUM(Amount)
FROM Opportunity
GROUP BY AccountId, Account.Name, Account.Website
Bit annoying that you have to list every field you want to see in GROUP clause too.
If you think you need this data in more places than this code (reports? list views?) then perhaps it's better idea to make a new "rollup summary" field on Account that'd aggregate for you the SUM(Amount)
I am guessing something like this:
SELECT Sum(o.Amount), a.AccountId, a.PersonName
FROM Opportunity o JOIN
Account a
ON a.Id = o.AccountId
GROUP BY a.AccountId, a.PersonName

joining tables in salesforce explorer

trying to join lead table with opportunity table in force.com explorer. New to soql. Here is what I have tried with error messages as results... can anyone help please?
SELECT CreatedDate, LeadSource, RecordTypeId, Name__c, StageName, HubSpot_Original_Source_Data_1__c, Name, HubSpot_Original_Source_Data_2__c, HubSpot_Original_Source_Type__c, (SELECT Company, IsConverted, CreatedDate, HubSpot_Inc__HubSpot_Intelligence__c, HubSpot_Original_Source_Data_1__c, HubSpot_Original_Source_Data_2__c, HubSpot_Original_Source_Type__c, LeadSource, RecordTypeId FROM Lead) FROM Opportunity where Name in (select Company from Lead)
Where clause of your query will not work. You can use another query in where clause only for checking matching Id.
SELECT CreatedDate, LeadSource, RecordTypeId, Name__c, StageName, HubSpot_Original_Source_Data_1__c, Name, HubSpot_Original_Source_Data_2__c, HubSpot_Original_Source_Type__c, (SELECT Company, IsConverted, CreatedDate, HubSpot_Inc__HubSpot_Intelligence__c, HubSpot_Original_Source_Data_1__c, HubSpot_Original_Source_Data_2__c, HubSpot_Original_Source_Type__c, LeadSource, RecordTypeId FROM Lead) FROM Opportunity where name = 'test'

Making a query that only shows unique records

I have a table where duplicate entries in one of the columns is possible (emailAddress - some couples share them) and I would like to send email newsletters to them. Is there a way to make a select query where it only shows one copy of the email address if there are multiple?
If you need only emailAddress it is quite simple:
select distinct emailAddress from <YourTableNameHere>
Edited according to request in comments.
If you want to choose both distinct emailAddress and ANY customerName related to it then you must somehow tell SQL how to choose the customerName. The easiest way is to select i.e. MIN(customerName), then all other (usually those that are later in alphabet but it actually depends on collation) are discarded. Query would be:
select emailAddress, min(customerName) as pickedCustomerName
from <YourTableNameHere>
group by emailAddress
You can use the DISTINCT keywprd, or you can GROUP BY.
SELECT DISTINCT email
FROM table
Or
SELECT email, Count(ID)
FROM table
GROUP By email

Resources