correlated-subquery

sql - multiple layers of correlated subqueries

倖福魔咒の 提交于 2019-12-13 16:11:47
问题 I have table A, B and C I want to return all entries in table A that do not exist in table B and of that list do not exist in table C. select * from table_A as a where not exists (select 1 from table_B as b where a.id = b.id) this gives me the first result of entries in A that are not in B. But now I want only those entries of this result that are also not in C. I tried flavours of: select * from table_A as a where not exists (select 1 from table_B as b where a.id = b.id) AND where not exists

How to merge these queries into 1 using subquery

こ雲淡風輕ζ 提交于 2019-12-13 09:51:49
问题 Select * from HotelPerson Where RoomID IN (select ID from HotelRoom Where BookingID = 36 ) Select * from HotelCancelationPolicy Where RoomID IN (select ID from HotelRoom Where BookingID = 36 ) How can I merge these both queries into 1 query ? 回答1: This will give you all the columns from both tables in one table. SELECT * FROM HotelPerson A, HotelCancelationPolicy B WHERE A.RoomID = B.RoomID AND A.RoomID IN (SELECT ID FROM HotelRoom WHERE BookingID = 36) 回答2: Use UNION to Get Distinct elements

How to solve the following case?

泄露秘密 提交于 2019-12-13 09:25:13
问题 Consider the following question: We have 3 tables, 1. Theatre ( theatre_Id, thatre_name ) 2. ShowTime ( showTimeId, theatre_Id, movie_id ) 3. Movies ( movie_id, movie_name ) Now same movie name can also have different movieId's sort of dependent on the reel. Eg: [1, HarryPotter], [2, HarryPotter], [3, Pirates of Carr] Now we need to find movie name which has showtime on all theatre locations ? Is it nested correlated query ? 回答1: If you better phrase the question: What are the names of movies

Bad (correlated) or Good (not correlated) subquery?

馋奶兔 提交于 2019-12-13 06:35:16
问题 I wonder, if the subquery in the following update statement is a good (not correlated) or bad (subquery)? In other words, my question is, is it an inefficient query? UPDATE tableA SET field1=0 FROM tableA WHERE field2 IN (SELECT field2 FROM tableA WHERE someField IS NOT NULL AND someOtherField = 'ABC') 回答1: Your query is not correlated ,its just a subquery.. below is a correlated subquery.. UPDATE a SET field1=0 FROM tableA a WHERE exists (SELECT 1 FROM tableB b WHERE a.somecol=b.somecol) one

correlated query /subquery VS join query

喜欢而已 提交于 2019-12-11 17:53:03
问题 can we always convert a usual subquery/correlated subquery to join type query? 回答1: yes and no... Yes, in that JOIN/EXISTS/IN usually give the same plan and are often the same No, in that if the EXISTS table has > 1 row for the main table you'll need DISTINCT to avoid dupes I'd stick with an EXISTS type query if this is relevant and you don't need data from the EXISTS table 回答2: Yes, but it means that what you JOIN to can appear in the result set. A subquery in either an EXISTS or IN clause

Update From Select with Correlated Subquery and Join in PostgreSQL

别说谁变了你拦得住时间么 提交于 2019-12-11 17:14:41
问题 I am migrating from SQL Server to Postgres and it has gone okay for the most part. One of the issues is that I am unable to figure out how to make this query work in Postgres: update "Measure" set DefaultStrataId = StrataId FROM (SELECT "Strata"."MeasureId", Min("Strata"."index") AS "Index" FROM "Strata", "Measure" WHERE "Strata"."MeasureId" = "Measure"."MeasureId" and "Strata"."StrataId" in (select strataid from point) GROUP BY "Strata"."MeasureId") a INNER JOIN strata ON "Strata"."index" =

Correlated SubQuery SQL to LINQ

守給你的承諾、 提交于 2019-12-11 17:13:30
问题 select * from Table1 where TC in (select TC from Table2 where Application in ('AAA'))` help me in converting above query to LINQ. 回答1: Without where Application in ('AAA') part this looks quite simple: from t1 in db.Table1s where db.Table2s.Select(t2 => t2.TC).Contains(t1.TC) from t1 in db.Table1s UPDATE (How wrong I was!) List<string> myCollection = new List<string> { "AAA" }; from t1 in db.Table1s where db.Table2s.Where(t2 => myCollection.Contains(t2.Application)).Select(t2 => t2.TC)

PostgreSQL: best way to join small subsets of large tables

别来无恙 提交于 2019-12-11 11:50:33
问题 I am using PostgreSQL 9.3 on a Windows machine. Let's say I have a clients table with 4 million records and an orders table with 25 million records. However, I am interested in my New York clients only. There are only 5,000 New York clients who placed 15,000 orders, i.e. a drastically smaller subset. What is the best way to retrieve the client ids and the total number of orders ever placed by New York clients? Is a correlated subquery like Select c.clientid , ( select count(orders.clientid)

joins versus correlated exists subqueries

烂漫一生 提交于 2019-12-11 04:09:40
问题 In Derby and/or MySQL, I am wondering about the performance of: select c0.documentid from contentblob as c0 where c0.documentid > ? and c0.blobid = ? and exists (select * from contentblob c1 where c1.documentid = c0.documentid and c1.blobid = ?) order by c0.documentid Note that DIFFERENT values will be supplied for the two blobid comparisons. What this query does is return the id's that occur in the table (at least) twice: once with blobid1 and once with blobid2. compared to the equivalent

Correlated Subquery in SQL

蓝咒 提交于 2019-12-11 03:56:41
问题 I have a database of radiology reports which I have mined for incidents of pulmonary nodules. Each patient has a medical record number and each procedure has a unique accession number. Therefore, a MRN can have multiple Accession numbers for difference procedures. Accession numbers are ascending, so if a patient has multiple accession numbers the largest accession number is the latest procedure. I need to: Identify the oldest (initial) study Find the next study which comes soonest after the