Limit SOQL query results from child clause - salesforce

Hopefully this will be easy to answer:
select name, id, description, isactive, productcode, imageurl__c, (select name, id, unitprice,Must_Override_Price__c, Is_Taxable__c from PricebookEntries where pricebook2id =: pbe.id) from product2
How do I prevent the product2 records that have no child pricebookentry records from being queried?
So lets say I have 10 products, but only two match the WHERE clause. I only want those two to show up so I'm not wasting resources/rows.

Perform the query relationship in reverse. You can still get all the required Product2 fields, and you will only get results for existing PricebookEntry records.
E.g.
select Id, Name, Pricebook2Id, Product2Id, UnitPrice, IsActive, UseStandardPrice,
ProductCode, IsDeleted,
Product2.Id, Product2.Name
from PricebookEntry
where pricebook2id = :pbe.id

You can do the query in reverse as mentioned in the Daniel's comment or try putting where condition in the top query as well
select name, id, description, isactive, productcode, imageurl__c,
(select name, id, unitprice,Must_Override_Price__c, Is_Taxable__c
from PricebookEntries
where pricebook2id =: pbe.id)
from product2
where Id IN (select Product2Id
from PricebookEntries
where pricebook2id =: pbe.id)

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

How to combine several rows into one row in SQL

I want to find the total amount of associates at a company, as well as how many female and male engineers all in one query.
I am able to just get the sum of all associates on one row when that is the only thing that my query is looking for, but as soon as I try and combine it with the query looking for the number of males an females, the associate job titles start to separate.
My current code looks like this:
SELECT
count(*) as [Number of Employees], Gender, Job
FROM
#table
WHERE
Job like '%Associate%'
GROUP BY grouping sets
((Job), (Gender))
the result sets have a row for each type of associate job, and I am trying to figure out how to combine them under one row under the name 'associate'
Here is the simple way
SELECT SUM(numberofemployees) AS total,
Gender,
CASE WHEN JobTitle LIKE '%Engineer%' THEN 'Engineer' ELSE JobTitle END AS JobTitle
FROM #table
GROUP BY Gender,
CASE WHEN JobTitle LIKE '%Engineer%' THEN 'Engineer' ELSE JobTitle END
OUTPUT:
total Gender JobTitle
5 F NULL
3 M NULL
8 NULL Engineer
Check this script. In addition, this will also return "Engineer" value in Job Title column for F & M. But if it is your requirement to have NULL there, some adjustment required in the script.
Note: Sub query is just for a better understanding. This can be also achieved in a single query.
SELECT COUNT(*),
GENDER,
JobTitle
FROM
(
SELECT
JobTitle,
Gender,
CASE
WHEN JobTitle like '%Engineer%' THEN 'Engineer'
ELSE JobTitle
END AS JobTitle
FROM HumanResources.EmployeeFROM
)A
WHERE A.JobTitle = 'Engineer'
GROUP BY GENDER,JobTitle
Add this:
SELECT
count(*) as [Number of Employees],
Gender,
JobTitle
FROM
HumanResources.Employee
WHERE
JobTitle like '%Engineer%'
Group by grouping sets
((JobTitle), (Gender))
HAVING
COUNT(*)>=3
Ok so for what you want
You can try this because
SELECT SUM(a.[Number of Employees]) AS Value1, a.Gender , a.JobGroup FROM (
SELECT 'Engineer' AS JobGroup, COUNT(*) AS [Number of Employees], Gender, JobTitle FROM HumanResources.Employee WHERE JobTitle LIKE '%Engineer%' GROUP BY grouping sets ((JobTitle), (Gender))) a
GROUP BY a.Gender,a.JobGroup

Salesforce REST API SOQL get account for opportunies

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

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'

SQL Server over query

My query returns the latest entry for each userid, but I need it to return the the latest entry for each userid and taskname. I tried to use group by, but I am getting an error. Is there something I'm doing wrong? Thanks!
SELECT UserId, TaskName, First, Last, email, ValueDate, Analysis
FROM (SELECT UserId, TaskName, First, Last,
email, ValueDate, Analysis,
ROW_NUMBER() OVER(PARTITION BY UserID
ORDER BY ValueDate DESC) AS rk
FROM MyTable) AS L
WHERE rk = 1
You should replace PARTITION BY UserID with PARTITION BY UserID, TaskName :
SELECT UserId, TaskName, First, Last, email, ValueDate, Analysis
FROM (SELECT UserId, TaskName, First, Last,
email, ValueDate, Analysis,
ROW_NUMBER() OVER(PARTITION BY UserID, TaskName
ORDER BY ValueDate DESC) AS rk
FROM MyTable) AS L
WHERE rk = 1
Without having some data to test and tables structure, I can only presume what you really want to achieve but it could be something that look like this :
SELECT UserId, TaskName, First, Last, email, max(ValueDate), Analysis
FROM MyTable
GROUP BY UserId, TaskName, First, Last, email, Analysis
ORDER BY MAX(ValueDate) DESC

Resources