joining tables in salesforce explorer - salesforce

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'

Related

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.

Sub Query with Aggregate Function in where clause not working in SOQL Salesforce

I have written a very simple SOQL query in Salesforce but it doesn't work.
SELECT Id, UserId, TerritoryId, IsActive
FROM UserTerritory
WHERE UserId NOT IN (SELECT UserId
FROM UserTerritory
GROUP BY UserId
HAVING COUNT(UserId) > 1)
The query above is throwing the following exception:
In Developer Console, it shows "Unknown error parsing query"
In Workbench, it shows:
MALFORMED_QUERY: (SELECT UserId FROM UserTerritory GROUP BY
UserId HAVING count(UserId) ^ ERROR at Row:1:Column:114 expecting a
right parentheses, found 'GROUP'
Such query doesn't make sense anyway.
Let's say that you have simplified the original SOQL as follows:
SELECT Id, UserId, TerritoryId, IsActive
FROM UserTerritory
WHERE UserId NOT IN (SELECT UserId FROM UserTerritory)
Salesforce won't let you do so and the exception will be thrown:
The inner and outer selects should not be on the same object type
If more thorough analysis is expected, additional details on the topic are needed (ideally, data examples).

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

Limit SOQL query results from child clause

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)

How to limit the answers of the GROUP BY clause in sql-server (2005)

I'm trying to get the top 50 cities for all customer lists in our DB
(So simplified: every client has a list of customers with associated data(like the city))
If I say:
SELECT top(50) clientid, city, COUNT(city) as cnt
FROM customers
GROUP BY clientid, city
ORDER by cnt
it will limit the total resultset on 50 rows instead of limiting the results for every group.
How can I get the top 50 per clientid?
EDIT:
I searched stackoverflow (and googled) but only found solutions for Mysql. Probably searching for 'limit' will only find mysql solutions wince thats the keyword needed for that Database engine. If I know the keyword needed in Sql-Server I could find it as well using google.
;WITH cte
As (SELECT clientid,
city,
COUNT(city) as cnt,
ROW_NUMBER() OVER (PARTITION BY clientid
ORDER BY COUNT(city)) AS RN
FROM customers
GROUP BY clientid,
city)
SELECT clientid,
city
FROM cte
WHERE RN <= 50
I think this might do it:
select top 50 city
from (select city from customers group by city order by count(clientid) desc)
I assume if a clientID is in the same row as a city, then that clientID represents a user living in that city.

Resources