I want to fetch a set of records from db where a field matches multiple values ( the count of which cant be predetermined ) . To exemplify,
Tables.A.ID.in(Set of IDs)
Tables.A.ID.notIn(Set of IDs)
I went through the documentation of fetchMany and fetchAny ResultQuery Documentation. I tried implementing it , but with no success.
I want to fetch all rows in DB which match the "Set of IDs" where IDs are NOT UNIQUE.
I am not able to understand how to use 'in' and 'notIn' in my pretext. Could someone show me with an example how to fetch the Set of Resulting Records from the database.
I suspect, you're simply looking for this?
Set<Integer> setOfIDs = ...
Result<Record> result =
DSL.using(configuration)
.select()
.from(A)
.where(A.ID.in(setOfIDs))
.fetch();
Related
I have a column in the users table of my Postgres dB table that is named email_subscription_prefs, which stores some some information in JSON format. It has an array_length of 1.
Sample Data:
[{"marketing":true,"transactional":true,"collaboration":true,"legal":true,"account":true}]
Issue:
I am trying to use bookshelf.js ORM to query and search all records in this table based on the value of the marketing key, specifically when its valueis true.
Here is an edited snippet of my code showing what I'm trying to implement this query using bookshelf.js:
return new User()
qb.where(function() {
this.where('domicile', 'USA').orWhere('domicile', null)
})
qb.whereRaw('cast(email_subscription_prefs->>? as boolean) = ?', ['marketing', true])
qb.limit(100)
})
Can someone tell me what I'm doing wrong on qb.whereRaw statement where I'm trying to query the JSON column email_subscription_prefs?
The code returns nothing where there are several thousands records in the users table.
Thanks in advance.
You seem to have an array of objects in sample data instead of single json object.
[
{
"marketing":true,
"transactional":true,
"collaboration":true,
"legal":true,
"account":true
}
]
so looks like you are trying to refer email_subscription_prefs->>'marketing' which is not found from the array.
To fetch marketing attribute of the first item in the array you should do:
email_subscription_prefs->0->>'marketing'
If that is not the problem, then you would need to add some example data from your DB to be able to tell what is the problem. You current description doesn't describe the queried table well enough.
So I want to return paged data from a query:
var data = context.MyTable.OrderByDescending(r => r.Field1)
.Skip(10)
.Take(10)
This will give me the second page of 10 rows order by Field1
But lets say all rows in the table have the same value for Field1, the data return by the Skip/Take is not correct. I've seen where the 2nd page may contain rows that where already returned in Page 1.
Note using EF 6.1.3
It would seem that to get the correct results, I need to ensure that the Ordering, results in a unique order of the data. So I add another column to the OrderBy, the Unique Id of the table.
var data = context.MyTable.OrderByDescending(r => r.Field1).ThenBy(r => r.FieldId)
.Skip(10)
.Take(10)
I've not found any documentation that confirms I need to do this, or is this a bug in EF?
I am trying this query:
List<Account> onlyRRCustomer = [SELECT
ac.rr_First_Name__c,
ac.rr_Last_Name__c,
ac.rr_National_Insurance_Number__c,
ac.id,
ac.rr_Date_of_Birth__c
FROM
Account ac
WHERE
ac.rr_National_Insurance_Number__c IN :uniqueNiInputSet
AND RecordTypeId = :recordTypeId];
It gives me an error:
SELECT ac.rr_First_Name__c, ac.rr_Last_Name__c,
ac.rr_National_Insurance_Number__c, ac.id, ac.rr_Date_of_Birth__c FROM
Account ac WHERE (ac.rr_National_Insurance_Number__c = :tmpVar1 AND
RecordTypeId = :tmpVar2) 10:12:05.0
(11489528)|EXCEPTION_THROWN|[49]|System.QueryException: Non-selective
query against large object type (more than 200000 rows). Consider an
indexed filter or contact salesforce.com about custom indexing.
I understand uniqueNiInputSet.size() ~ 50, so, it's not an issue but for that record type, it might contains more records.
So, if i changed the position will that work? Means, first the recordtype and then the NIset in where clause. Is there any order how where clause are selected in SF. So, it will only look for 50 member and then within 50 it will serach for the particular record type?
That just means that the script is taking too long to execute. You may need to move this to a #future method or make execute it using Database.Batchable.
I don't think the order matters in SOQL, I think it's just trying to return too many records.
A non-selective query means you are performing a query against a table that has a large number of records and your query is not specific enough. You can work with Salesforce support to try to resolve this, either through the creation of additional backend indexes or by making the query more selective.
To be honest, your query looks very selective already, you're not using LIKE or IN. You should also put your most selective conditions first (resulting in a more focused query against your records).
I know it should'nt matter, but I would also move your conditions out of the parenthesis.
If there are any other fields you can filter on, that may help. Sometimes, you have to actually create new fields and populate them just to help make your queries more selective.
Also, if rr_National_Insurance_Number__c is a formula field, you will want to change it to a text field and populate workflow or apex instead. Formula fields require additional time on the servers to calculate.
SELECT rr_First_Name__c, rr_Last_Name__c, rr_National_Insurance_Number__c, id, rr_Date_of_Birth__c
FROM Account
WHERE new_custom_field__c = TRUE
AND rr_National_Insurance_Number__c = :tmpVar1
AND RecordTypeId = :tmpVar2
Your query is non-selective. For a standard indexes is 30% for the fist million records and 15% of records over a million up to 1 million records total. For and "AND" query each individual where criteria must itself be selective see this quick reference cheat sheet. In general try making
rr_National_Insurance_Number__c
an external id which will make it an indexed by salesforce by default and retry you query. Record Types are already indexed by default. If the result is still non-selective because of the number of results returned, try limiting the number of results using a field like CreatedDate to limit the scope of the query.
I'm using the following query to fetch records from database. The row that exists in database and the column question contains: "How are we going to do solve this. Is it complex?"
I'm using the following query to fetch the data.
SELECT * FROM TRENDING WHERE question ILIKE '%how are we%' ;
The issue is that, the above query works only if those three words are present in database. If I use additional word as following, the query does not work. For instance, the following query does not work
SELECT * FROM TRENDING WHERE question ILIKE '%how are we doing %' ;
How can I sort this out? I want it to fetch any record that might contain these words that are being queried?
I have a table HistoryRecords which has two columns recordName and timeStamp. I have to delete the records based on both of these. I had used the following query to delete 2 records <'abc', '2010/10/20 19:39:20.0'> and <'def', '2010/10/25 17:43:3.0'> :
DELETE FROM HistoryRecords WHERE (recordName IN (N'abc',N'def') AND timeStamp IN (N'2010/10/20 19:39:20.0',N'2010/10/25 17:43:3.0'))
The problem is that the above query leads to deletion of other records also like <'abc', '2010/10/25 17:43:3.0'> because the list of recordNames contains 'abc' and list of timestamp contains '2010/10/25 17:43:3.0'.
Please let me know any approach that will prevent this extra unintended deletion.
You should seprate cases. Dont combine
delete from historyRecords
where (recName='abc' and timeStamp='2010...19:39')
OR
(recName='def' and timeStamp='2010...17:43')