Not able to return a field from a child of a parent in SOQL - sql

I have tested the following query which is successful but when apply the query to my custom object it fails. i need to find out what is causing the issue.
SELECT Id, Account.Name FROM Contacts WHERE AccountId in (SELECT Id FROM Account)
Background: There are two entities: Customers and Visits, there can be many visits for any customer. I have created a custom object Customer_Visits__c with several fields. It has a lookup field called Customer__c which looks up to Account (sObject) and stores the Account Id field. The relationship name is CustomerVisitsAccounts. I want to be able to return the customer name field (Account.Name) in the SOQL query, i.e., for each visit record show the name of the customer.
It get success with:
SELECT Id, Name FROM Customer_Visits_c WHERE Customer_c in (SELECT Id FROM Account)
Here is the SOQL string I get fails with:
SELECT Id, Name, CustomerVisitsAccounts__r.Name FROM Customer_Visits_c WHERE Customer_c in (SELECT Id FROM Account)
Here is the error message I get:
ERROR at Row:1:Column:18
Didn't understand relationship 'CustomerVisitsAccounts_r' in field path. If you are attempting to use a custom relationship, be sure to append the '_r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.

Related

How to get a row with no value in sql

I have a table with every campaign, that table has it's name and the number of emitted coupons. The table client_campaign connects the campaign with the client (campaign_id and client_id), so if a client subscribes a campaign, he gets a coupon.
I want to get a query with the number of emitted and used coupons. I started of with this:
select name, emitted_coupons, count(campaign_id) as used_coupons
from campaign, client_campaign
where campaign.campaign_id = client_campaign.campaign_id
group by name, emitted_coupons;
The problem is that my implementation doesn't show the campaigns where no coupon was used. I would like to know if it's possible to do all this using only one query and not changing the tables.
This can be done by using the NULL keyword:
SELECT * FROM table_name WHERE value IS NULL;

Oracle query - How to check if a user is the only one assigned to a project

I am trying to determine if a user is assigned to any project with a specific role (ProjMemberTypeID) and if so, if s/he is the only one.
There is "projMember" table with Project ID, User ID and Project Member Type ID. I need to know if there are any project IDs where this user is the only one with member type id of 1.
Is it also possible to get some project info (like name, ...) joining the requested query with "Project" table, based on Project ID?
I tried this but it only gives me how many users are assigned to projects and if I filter by user ID I get how many projects user is assigned to but don't know how to check if s/he is the only one.
select count(userid), projectid
from projmember
where projmembertypeid = 1 -- and userid=73
group by projectid
order by projectid;
You can use a having clause:
select projectid
from projmember
where projmembertypeid = 1
group by projectid
having min(userid) = 73 and max(userid) = min(userid)
order by projectid;
The query filters on the relevant member type, then aggregates by project; all that is left to do is ensure that the given user was found, and no other.

MS Access SQL code - query issue with NOT IN

I'm trying to find out which partners has not paid the monthly tuition in a particular month.
I have a table called Socios containing all partners names SocioNome and another table called RegistroPagamento contaning all payments done (This particular table is fulfilled by a form where the user input the Partner Name, Amount Paid and which particular month/year the payment is related to).
I have created a query where I used the SQL code below:
SELECT [SocioNome]
FROM [Socios] NOT IN
(SELECT [SocioNome] FROM [RegistroPagamento] WHERE [MesBoleto] = [Forms]![Selecionar_MCobranca]![TBoxMes] AND [AnoBoleto] = [Forms]![Selecionar_MCobranca]![TBoxAno]);
[Selecionar_MCobranca] is the form I have mentioned before and the [TBoxMes] & [TBoxAno] are the combo boxes from the form which the user can select the month and the year the payment refers to.
When I run the code, a error message pops up indicating that there is a FORM clause syntax issue, and I don't know exactly what is causing the problem.
NOT IN is a comparison operator in the WHERE clause. It does not belong in the FROM cluase. I strongly recommend using NOT EXISTS instead. The idea is:
SELECT s.SocioNome
FROM Socios as s
WHERE NOT EXISTS (SELECT 1
FROM RegistroPagamento as rp
WHERE rp.MesBoleto = [Forms]![Selecionar_MCobranca]![TBoxMes] AND
rp.AnoBoleto = [Forms]![Selecionar_MCobranca]![TBoxAno] AND
rp.SocioNome = s.SocioNome
);
NOT IN returns no rows if any row in the subquery is NULL. To protect against this, just use NOT EXISTS. It has the expected behavior in this case.

Retrieve count of related records

I currently have two tables in my data source that I'm referencing in this instance.
Firstly, to explain the context, I have a windows form program in VB.NET (Visual Studio 2013).
In the first table Trainer, I have the following fields : ID, First Name, Surname, Contact, Class.
In the second table Member, I have the following fields : ID, First Name, Surname, Contact, Type, TrainerID.
I have enforced referential integrity between the two tables using Trainer.ID as PK and Member.TrainerID as FK with a 1:m relationship. I'm trying to retrieve the count of related records, to the specified ID of the trainer. I want to retrieve the count of related records. So, for example on the form I click Search and provide a trainer ID, I'd like to return the amount of customers he/she has belonging to them.
I need that, so that I can work out their salary based on commission + base amount.
I've looked around a lot, read up a lot but I just can't seem to get it. Any help whatsoever will be appreciated.
If you have the trainer id, can't you just do:
select count(*) as cnt
from member m
where m.trainerid = #TheTrainerId;
select count(m.id) as count_members
from trainer t
left join member m on m.trainerid = t.id
where t.surname = 'watson'

Salesforce SOQL query to find an opportunity, given its parent account id?

How can you write a SOQL (Salesforce query language) query to find an opportunity, given its parent account's account id?
I want to do something like this (doesn't work though):
SELECT Id, Name FROM Opportunity WHERE AccountId = '003D000000f0gfH'
Your query is fine, but you don't have an AccountId, 003 prefixed ids are for contacts.