For the given SOQL query, trying to optimize SOQL Query. I have two queries
Query 1 to get userID from salesforce for the given user account
SELECT id From User WHERE Username = 'test#testmail.com'
Query 2 for the given user ID find custom objects
SELECT Id,Headline__c,Description__c
FROM Incident__c
WHERE Id IN (SELECT Incident__c FROM Node_Affected__c
WHERE (Incident__r.Account__c = '034524000000uzurX')
AND Incident__r.Indicator__c = false)
Now I want to optimize it to one query such that I dont have to make two api calls from external system. So I was trying
SELECT Id,Headline__c,Description__c
FROM Incident__c
WHERE Id IN (SELECT Incident__c FROM Node_Affected__c
WHERE (Incident__r.Account__c = null OR Incident__r.Account__c IN (SELECT id From User WHERE Username = 'test#testmail.com'))
AND Incident__r.Indicator__c = false)
But I get the following error
MALFORMED_QUERY:
Incident__r.Account__c IN (SELECT id From User WHERE Username = 'test#testmail.com'))
^
ERROR at Row:5:Column:87
Nesting of semi join sub-selects is not supported
Is there any better way to solve this type of problem? Thanks in advance :)
You should be able to use the SOQL feature to follow foreign keys and follow the account__c relationship to the user table and filter on username from there, e.g.
SELECT Id, Headline__c, Description__c
FROM Incident__c
WHERE Id IN (SELECT Incident__c FROM Node_Affected__c
WHERE (Incident__r.Account__c = null OR
Incident__r.Account__r.Username = 'test#testmail.com')
AND Incident__r.Indicator__c = false)
Related
I am trying to select from one table using the select result from another table. I can run this in two queries but would like to optimize it into just one.
First query.. Select ids where matching other id
select id from lookuptable where paid = '547'
This results in something like this
6316352
6316353
6318409
6318410
6320468
6320469
6320470
6322526
6322527
6324586
6324587
6326648
I would like to then use this result to make another selection. I can do it manually like below. Note, there could be many rows with these values so I've been using a IN statement
select * from "othertable" where id in (6316352,6316353,6318409,6318410,6320468,6320469,6320470,6322526,6322527,6324586,6324587,6326648);
select
ot.*
from
"othertable" as ot
join
lookuptable as lt
on
ot.id = lt.id
where
lt.paid = '547'
The IN operator supports not just value lists but also subqueries, so you can literally write
select * from "othertable" where id in (select id from lookuptable where paid = '547');
issue:
I have an sql query that works with a custom "specialfield1" in transactionline table:
{ "q": "SELECT * FROM transaction t LEFT JOIN transactionline tl ON t.id = tl.transaction where tl.specialfield1 IS NOT NULL and t.trandate >= '11/1/2021'"
}
However I need to also get the account number for these results.
I am not sure what field to match with the account table using the transaction - transactionline table???
Q: Can you let me know how to also retrieve the account number for this query using additional query matches and tables (account table)?
Thanks
You need to join with Accounts table
SELECT A.NAME,* FROM transaction t LEFT JOIN transactionline tl ON t.id = tl.transaction
Inner join Accounts A on TL.ACCOUNT_ID=A.ACCOUNT_ID
where tl.specialfield1 IS NOT NULL and t.trandate >= '11/1/2021'
I just can't figure it out why this query work
SELECT id, name, organization_id
FROM facilities
WHERE organization_id = ANY(
'{abc-xyz-123,678-ght-nmp}'
)
But this query wont work with error operator does not exist: uuid = uuid[]
SELECT id, name, organization_id
FROM facilities
WHERE organization_id = ANY(
SELECT organization_ids
FROM admins
WHERE id = 'jkl-iop-345'
)
When the subquery
SELECT organization_ids
FROM admins
WHERE id = 'jkl-iop-345'
give the exact result of {abc-xyz-123,678-ght-nmp}.
I'm using postgres (PostgreSQL) 13.3
The subquery produces one row that contains an array.
If you use = ANY (SELECT ...), the result set is converted to an array, so you end up with
{{abc-xyz-123,678-ght-nmp}}
which is an array of arrays.
You probably want
SELECT id, name, organization_id
FROM facilities
WHERE EXISTS (SELECT 1 FROM admins
WHERE admins.id = 'jkl-iop-345'
AND facilities.organization_id = ANY (admins.organization_ids)
);
Let me remark that storing references to other tables in an array, JSON or other composite data type is an exceptionally bad idea. A normalized schema with a junction table would serve you better.
I'm using postgresql database and have a log table. I want to show the ticket information from this log table and want to sort by id. But ticket id has duplicate data, so I use distinct to filter, and then I can't sort by id when I use distinct.
How can I solve this issue? Thanks!
rows = db(db.log.ticket_id != '').select(db.log.ALL, orderby=~db.log.id, distinct=db.log.ticket_id , limitby=((page-1) * PAGE_ROWS, (page*PAGE_ROWS)))
I got the error message:
SELECT DISTINCT ON expressions must match initial ORDER BY expressions
And I try to:
rows = db(db.log.ticket_id != '').select(db.log.ALL, orderby=~db.log.id|db.log.ticket_id, distinct=db.log.ticket_id , limitby=((page-1) * PAGE_ROWS, (page*PAGE_ROWS)))
But still can't work...
Related to my previous post here, I have the following SELECT:
SELECT tc.[Time],tc.[From], tc.[To], tc.[Cost], tc.[Length], tc.[Type], tc.[PlaceCalled]
FROM
TelstraCall as tc
WHERE
[AccountNumber] IN (#AccountNumber)
ORDER BY [Time] DESC
I'm trying to get the [Username] out of [Resource] given that the [PhoneNum] in [rtc] matches either [From] or [To], and Hogan has kindly helped me out with the first half :
USE [rtc]
SELECT [Username]
FROM [dbo].[Resource] R
JOIN ResourcePhone RP on R.ResourceId = RP.ResourceId
WHERE RP.PhoneNum = tc.[From]
Now I'm trying to work out the syntax of how to get a 'User1' given that [From] matches the [PhoneNum] in [rtc] and a 'User2' if [To] matches [PhoneNum] instead, because I can't have them being jumbled up.
What you're wanting to do is join on the same table twice to get related values based on two different references.
For this, you use table aliases. Here's a simple example
SELECT u1.[Username] AS User1, u2.[Username] AS User2
FROM TelstraCall tc
INNER JOIN ResourcePhone rp1 ON tc.[From] = rp1.PhoneNum
INNER JOIN Resource u1 ON rp1.ResourceId = u1.Id -- guessing at column names here
INNER JOIN ResourcePhone rp2 ON tc.[To] = rp2.PhoneNum
INNER JOIN Resource u2 ON rp2.ResourceId = u2.Id
Here is one way that you can do this using CROSS APPLY since you are using SQL Server 2008. CROSS APPLY helps you to join your table with sub queries.
In this case, the table CallDetails in the database PhoneBills drives your query using the fields From and To. Both these fields have to fetch the Username data from the table Resource in the database rtc by joining with the PhoneNumber column in the table ResourcePhone also in the database rtc.
So the inner/sub query will join the tables Resource and ResourcePhone, it will then be used twice to fetch User1 and User2. For User1, the filter will use the From field in the table CallDetails in the database PhoneBills and for User2, the filter will use the To field in the table CallDetails in the database PhoneBills
SELECT USR1.UserName AS [User1]
, USR2.UserName AS [User2]
FROM PhoneBills.dbo.CallDetails CD
CROSS APPLY (
SELECT Username
FROM rtc.dbo.Resource R
INNER JOIN rtc.dbo.ResourcePhone RP
ON RP.ResourceID = R.ResourceID
WHERE RP.PhoneNumber = CD.From
) USR1
CROSS APPLY (
SELECT Username
FROM rtc.dbo.Resource R
INNER JOIN rtc.dbo.ResourcePhone RP
ON RP.ResourceID = R.ResourceID
WHERE RP.PhoneNumber = CD.To
) USR2