subquery

Is it a slow query? Can it be improved?

守給你的承諾、 提交于 2019-12-22 05:57:54
问题 I was going through SQLZOO "SELECT within SELECT tutorial" and here's one of the queries that did the job (task 7 ) world(name, continent, area, population, gdp) SELECT w1.name, w1.continent, w1.population FROM world w1 WHERE 25000000 >= ALL(SELECT w2.population FROM world w2 WHERE w2.continent=w1.continent) My questions are about effectiveness of such query. The sub-query will run for each row (country) of the main query and thus repeatedly re-populating the ALL list for a given continent.

Linq with Left Join on SubQuery containing Count

a 夏天 提交于 2019-12-22 04:19:08
问题 I'm having difficulty translating sql to linq syntax. I have 2 tables (Category and CategoryListing) which reference each other with CategoryID. I need to get a list of all the CategoryID in Category Table and the Count of CategoryID for all corresponding matches in the CategoryListing table. If a CategoryID is not present in CategoryListing, then the CategoryID should still be returned - but with a frequency of 0. The following sql query demonstrates expected results: SELECT c.CategoryID,

Views vs. inline subqueries SQL Server 2005/2008

烂漫一生 提交于 2019-12-22 00:25:30
问题 In certain areas in my application I need data from several tables in the database (I have an application, the application has many attributes, each attribute has several definitions, and each definition has a value). I need the data from all these tables for an application. Should I use a view (one that would be rather large) or subqueries for selecting the records? In the case of subqueries is the optimizer able to work efficiently? I'm also interested if caching will work for subqueries.

Updating Variable with Current Row Value

我的梦境 提交于 2019-12-21 22:17:50
问题 I am trying to perform a complex operation where I pull the sum for an entire column of data and subtract the running subtotal from the sum for each row. I can do the component parts of Sum and Running Subtotal alone. Used this for running subtotal: sum(UsageMetric) over(order by Nested1.IDNumber) as RunningTotal However, I get this error when trying to comine them: Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery

LINQ: Paging technique, using take and skip but need total records also - how to implement this?

爱⌒轻易说出口 提交于 2019-12-21 18:43:08
问题 I have implemented a paging routine using skip and take. It works great, but I need the total number of records in the table prior to calling Take and Skip. I know I can submit 2 separate queries. Get Count Skip and Take But I would prefer not to issue 2 calls to LINQ. How can I return it in the same query (e.g. using a nested select statement)? Previously, I used a paging technique in a stored procedure. I returned the items by using a temporary table, and I passed the count to an output

Hibernate subquery detachedCriteria

人盡茶涼 提交于 2019-12-21 10:32:10
问题 How to write a subquery in hibernate which is having multiple subqueries. for example select * from project_dtls where project_id in (select project_id from project_users where user_id = (select user_id from user_dtls where email='abc@email.com')) I know that we can write through DetachedCriteria but couldnot find any example where I can use multiple subqueries. 回答1: Here's an example: DetachedCriteria exampleSubquery = DetachedCriteria.forClass(MyPersistedObject.class) .setProjection

MYSQL use 'LIKE' in 'WHERE' clause to search in subquery

笑着哭i 提交于 2019-12-21 07:24:20
问题 How would you use 'LIKE' to search in a subquery? E.g. i've tried doing this, but doesn't work: SELECT * FROM mytable WHERE name LIKE '% (SELECT name FROM myothertable) %' I have this so far: SELECT * FROM t1 WHERE t1.name IN (SELECT t2.name FROM t2) AND (t1.title IN (SELECT t2.title FROM t2) OR t1.surname IN (SELECT t2.surname FROM t2)) It's working ok as it returns exact matchs, but it doesn't seem to return my other records that are similar, so I would like to also check that: t1.title

Concatenate (glue) where conditions by OR or AND (Arel, Rails3)

 ̄綄美尐妖づ 提交于 2019-12-21 04:57:24
问题 I have several complex queries (using subqueries, etc...) and want to glue them together with OR or AND statement. For example: where1=table.where(...) where2=table.where(...) I would like something like where3=where1.or where2 Next example doesn't work for me: users.where(users[:name].eq('bob').or(users[:age].lt(25))) because of I have several where(..) queries and I want to concatenate them . In other words I have 3 methods: first return first where, second-second, third - OR concatenation.

Calculating difference from previous record

ε祈祈猫儿з 提交于 2019-12-21 04:55:35
问题 May I ask for your help with the following please ? I am trying to calculate a change from one record to the next in my results. It will probably help if I show you my current query and results ... SELECT A.AuditDate, COUNT(A.NickName) as [TAccounts], SUM(IIF((A.CurrGBP > 100 OR A.CurrUSD > 100), 1, 0)) as [Funded] FROM Audits A GROUP BY A.AuditDate; The query gives me these results ... AuditDate D/M/Y TAccounts Funded -------------------------------------------- 30/12/2011 506 285 04/01/2012

How to make a “distinct” join with MySQL

我只是一个虾纸丫 提交于 2019-12-20 17:26:30
问题 I have two MySQL tables (product and price history) that I would like to join: Product table: Id = int Name = varchar Manufacturer = varchar UPC = varchar Date_added = datetime Price_h table: Id = int Product_id = int Price = int Date = datetime I can perform a simple LEFT JOIN: SELECT Product.UPC, Product.Name, Price_h.Price, Price_h.Date FROM Product LEFT JOIN Price_h ON Product.Id = Price_h.Product_id; But as expected if I have more than one entry for a product in the price history table,