subquery

SQL Server performance - Subselect or Inner Join?

↘锁芯ラ 提交于 2019-12-14 03:51:11
问题 I've been pondering the question which of those 2 Statements might have a higher performance (and why): select * from formelement where formid = (select id from form where name = 'Test') or select * from formelement fe inner join form f on fe.formid = f.id where f.name = 'Test' One form contains several form elements, one form element is always part of one form. Thanks, Dennis 回答1: The performance depends on the query plan choosen by the SQL Server Engine. The query plan depends on a lot of

MySQL if statement subquery value

不打扰是莪最后的温柔 提交于 2019-12-14 03:28:51
问题 I want to do something like this: SELECT IF((SELECT something FROM table) AS tmp) > 0, tmp, (SELECT bla FROM oter_table)) Unfortunately the 'as tmp' and returing the tmp is not working. How can I get this to work? without doing repeating the query: SELECT IF((SELECT something FROM table) > 0, (SELECT something FROM table), (SELECT bla FROM oter_table)) 回答1: You can use User-Defined Variables: SELECT IF( @val:=(SELECT something FROM table1) > 0, @val, (SELECT bla FROM table2) ) SQL Fiddle 回答2:

MySQL where clause and ordering by avg() as a sub query

Deadly 提交于 2019-12-14 02:18:47
问题 Although I can group and order by on an aliased sub query, I can't use the alias in a where clause. Do I need to use a join instead? Works: SELECT entries.*, (SELECT avg(value) FROM `ratings` WHERE ratings.entry_id = entries.id) as avg_rating FROM `entries` ORDER BY avg_rating DESC Fails ("unknown column 'avg_rating' in where clause"): SELECT entries.*, (SELECT avg(value) FROM `ratings` WHERE ratings.entry_id = entries.id) as avg_rating FROM `entries` WHERE avg_rating < '4.5000' ORDER BY avg

ACCESS 2010 SQL— using WHERE IN on SELECT TOP subquery field

妖精的绣舞 提交于 2019-12-14 01:26:00
问题 I want this query to tell me the spending in 2012 of the companies who were the top 10 spenders in 2013! SELECT [Company], Sum([SPENDING]) FROM [Data] WHERE [Company] IN ( SELECT TOP 10 [Company] FROM [Data] WHERE [Year] IN ("2013") GROUP BY Company ORDER BY Sum([SPENDING]) DESC ) AND [Year] IN ("2012") GROUP BY Company ; When I try to run it, I get no errors, but Access says it is "running query" and never finishes. The size of the data is not the problem. This is the closest example I found

Fluent NHibernate QueryOver to select items not in another table (left join)

偶尔善良 提交于 2019-12-13 16:10:46
问题 I have two tables: all: id | propA | propB | someOtherColumn hidden: id | propA | propB and corresponding classes (Mapped, no relationship mapped yet) I would like to get all rows from first table, minus any results which match by propA or propB property. I managed to do it by Criteria API but would like to see how it is done with QueryOver API, if possible without subqueries but with a left excluding join. Criteria version: var dc1 = DetachedCriteria.For<hidden>() .Add(Restrictions.IsNotNull

oracle subquery in keyword is slow on 12c

醉酒当歌 提交于 2019-12-13 16:09:47
问题 compared to 11g, 12c (currently using 12.1.0.2 version) in keyword is so slow. SELECT * FROM DATA_TABLE WHERE OID IN ( SELECT OID FROM ID_TABLE WHERE (condition) ) result 11g : under 1 sec 12c : over 10 sec below query is fast enough in both 11g and 12c (to let you know real problem is 'in subquery' query SELECT OID FROM ID_TABLE WHERE (condition) I can solve this problem with changing query as below SELECT * FROM DATA_TABLE D, ( SELECT OID FROM ID_TABLE WHERE (condition) ) O WHERE D.OID = O

Moving Average / Rolling Average

爱⌒轻易说出口 提交于 2019-12-13 15:42:16
问题 I have 2 columns in MS SQL one is Serial no. and other is values. I need the thrird column which gives me the sum of the value in that row and the next 2. Ex SNo values 1 2 2 3 3 1 4 2 5 6 7 9 8 3 9 2 So I need third column which has sum of 2+3+1, 3+1+2 and So on, so the 8th and 9th row will not have any values: 1 2 6 2 3 6 3 1 4 4 2 5 5 1 6 7 2 7 8 3 9 2 Can the Solution be generic so that I can Varry the current window size of adding 3 numbers to a bigger number say 60. 回答1: Here is the SQL

Suqueries in select clause with JPA

白昼怎懂夜的黑 提交于 2019-12-13 15:22:58
问题 I need to execute a subquery in a select clause with Apache Openjpa 2. Does JPA support subqueries in SELECT clause? My Query is something like this: SELECT t.date, t.value, (SELECT COUNT(DISTINCT t2.value2) FROM table t2 WHERE t2.date = t.date) FROM table t WHERE ... When I execute my query, I get a class cast exception: Exception in thread "main" <openjpa-2.1.1-SNAPSHOT-r422266:1141200 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: at org.apache.openjpa.kernel

Are subqueries evil?

不想你离开。 提交于 2019-12-13 15:22:57
问题 This question comes after a friend's comment. He said that when a query has a lot of subqueries, it's a signal that the database has design flaws and they must be avoided. He also said that many books suggest the same. I agree in parts, but I think that are queries that have complex logic that a lot of subqueries are needed, or, to avoid the subqueries, a materialized view of a query or a lot of data redundancy. So, what is the truth about subqueries? Must they always be avoided? No problems

mysql Select from Select

喜夏-厌秋 提交于 2019-12-13 13:12:09
问题 I have this query: SELECT DATE( a.created_at ) AS order_date, count( * ) as cnt_order FROM `sales_order_item` AS a WHERE MONTH( a.created_at ) = MONTH( now())-1 GROUP BY order_date which will return result something like this (snapshot only otherwise will return per 31 days): order_date cnt_order 2012-08-29 580 2012-08-30 839 2012-08-31 1075 and my full query is selecting based on above selection: SELECT order_date , MAX(cnt_order) AS highest_order FROM ( SELECT DATE (a.created_at) AS order