not-exists

Django Exists() / ~Exists() return if there is no matching data?

五迷三道 提交于 2020-03-25 18:56:13
问题 EDIT: As per schillingt's answer below I have switched to using Case/When: context['db_orders'] = Order.objects.filter( retailer_code=self.object.retailer_code).annotate( in_db=Case(When(Q(Subquery(self.object.suppliers.filter( supplier_code=(OuterRef('supplier_code'))) ), then=Value(True), default=Value(False), output_field=NullBooleanField())))) However I'm now struggling with an errror: FieldError at /retailers/A001/ Cannot resolve expression type, unknown output_field Original question: I

SQL do not insert duplicates

跟風遠走 提交于 2020-01-15 05:55:28
问题 I have been looking for a solution to best implement a do not insert if the line already exists. I've read a lot of answers which al differ in some way and it's a little above my level to make any sense. For example the following post is the top result: SQL Server Insert if not exist But I cannot understand why the variables are used and how I define table1 and table2. Would somebody be able to explain this a litle further so that I can apply it to my situation? In my example I have about 5

SQL - improve NOT EXISTS query performance

夙愿已清 提交于 2020-01-01 01:53:06
问题 Is there a way I can improve this kind of SQL query performance: INSERT INTO ... WHERE NOT EXISTS(Validation...) The problem is when I have many data in my table (like million of rows), the execution of the WHERE NOT EXISTS clause if very slow. I have to do this verification because I can't insert duplicated data. I use SQLServer 2005 thx 回答1: Make sure you are searching on indexed columns, with no manipulation of the data within those columns (like substring etc.) 回答2: Off the top of my head

MySQL Join Where Not Exists

无人久伴 提交于 2019-12-28 03:32:08
问题 I have a MySQL query that joins two tables Voters Households they join on voters.household_id and household.id Now what i need to do is to modify it where the voter table is joined to a third table called elimination, along voter.id and elimination.voter_id, how ever the catch is that i want to exclude any records in the voter table that have a corresponding record in the elimination table. how do i craft a query to do this? this is my current query SELECT `voter`.`ID`, `voter`.`Last_Name`,

MySQL Join Where Not Exists

纵饮孤独 提交于 2019-12-28 03:32:07
问题 I have a MySQL query that joins two tables Voters Households they join on voters.household_id and household.id Now what i need to do is to modify it where the voter table is joined to a third table called elimination, along voter.id and elimination.voter_id, how ever the catch is that i want to exclude any records in the voter table that have a corresponding record in the elimination table. how do i craft a query to do this? this is my current query SELECT `voter`.`ID`, `voter`.`Last_Name`,

(SQL) Replaced NOT IN with NOT EXISTS and results differ

ぐ巨炮叔叔 提交于 2019-12-23 05:44:27
问题 Trying to fix someone else's code. The NOT IN kills performance. I took it out and replaced with Not Exists and I'm getting different results. The commented out not in is just above my not exists. Anyone see anything stupid I'm doing here? IF @ProcessComplete = 1 BEGIN -- PRINT 'Group-Complete' INSERT INTO @ProcessIDTable SELECT DISTINCT(ProcessID) FROM vPortalInbox WHERE GroupUserIDs LIKE '%,' + CAST(@UserID AS VARCHAR(MAX)) + ',%' AND StepOwnerID IS NULL --AND ProcessID NOT IN (SELECT

SQL - not exists query with millions of records

自闭症网瘾萝莉.ら 提交于 2019-12-22 22:50:08
问题 I'm trying to use the following SQL query (in SAS) to find any records from pool1 that do not exist in pool2 . Pool1 has 11,000,000 records, pool2 has 700,000. This is where I run into an issue. I let the query run for 16 hours and it was nowhere near finishing. Is there a more efficient way (in SQL or SAS) to achieve what I need to do? PROC SQL; CREATE TABLE ALL AS SELECT A.ID FROM POOL1 A WHERE NOT EXISTS (SELECT B.ID FROM POOL2 B WHERE B.ID = A.ID); QUIT; 回答1: PROC SQL; CREATE TABLE ALL AS

Using a table variable inside of a exists statement

核能气质少年 提交于 2019-12-21 07:36:20
问题 I am trying to update a column inside of a table variable based on a condition, the condition being that the ID of the table variable does not exist in a different table: DECLARE @BugRep TABLE(BugCode VARCHAR(50),DevFirstName VARCHAR(50), DevLastName VARCHAR(50), BugDate VARCHAR(20), IsValid VARCHAR(1)) UPDATE @BugRep SET IsValid = 'N' WHERE NOT EXISTS(SELECT * FROM BUG b WHERE @BugRep.BUGCODE = b.CODE) When i try to compile the procedure that has these statements, I get a "Must declare the

Mysql select where not in table

五迷三道 提交于 2019-12-17 07:10:02
问题 I have 2 tables (A and B) with the same primary keys. I want to select all row that are in A and not in B. The following works: select * from A where not exists (select * from B where A.pk=B.pk); however it seems quite bad (~2 sec on only 100k rows in A and 3-10k less in B) Is there a better way to run this? Perhaps as a left join? select * from A left join B on A.x=B.y where B.y is null; On my data this seems to run slightly faster (~10%) but what about in general? 回答1: I use queries in the

Array.push() if does not exist?

寵の児 提交于 2019-12-17 02:07:50
问题 How can I push into an array if neither values exist? Here is my array: [ { name: "tom", text: "tasty" }, { name: "tom", text: "tasty" }, { name: "tom", text: "tasty" }, { name: "tom", text: "tasty" }, { name: "tom", text: "tasty" } ] If I tried to push again into the array with either name: "tom" or text: "tasty" , I don't want anything to happen... but if neither of those are there then I want it to .push() How can I do this? 回答1: You could extend the Array prototype with a custom method: /