correlated-subquery

How to calculate a moving average in MySQL in a correlated subquery?

送分小仙女□ 提交于 2019-12-11 00:49:51
问题 I want to create a timeline report that shows, for each date in the timeline, a moving average of the latest N data points in a data set that has some measures and the dates they were measured. I have a calendar table populated with every day to provide the dates. I can calculate a timeline to show the overall average prior to that date fairly simply with a correlated subquery (the real situation is much more complex than this, but it can essentially be simplified to this): SELECT c.date , (

DELETE SQL with correlated subquery for table with 42 million rows?

一笑奈何 提交于 2019-12-08 20:19:59
问题 I have a table cats with 42,795,120 rows. Apparently this is a lot of rows. So when I do: /* owner_cats is a many-to-many join table */ DELETE FROM cats WHERE cats.id_cat IN ( SELECT owner_cats.id_cat FROM owner_cats WHERE owner_cats.id_owner = 1) the query times out :( (edit: I need to increase my CommandTimeout value, default is only 30 seconds) I can't use TRUNCATE TABLE cats because I don't want to blow away cats from other owners. I'm using SQL Server 2005 with "Recovery model" set to

How to reduce many similar correlated subqueries?

大憨熊 提交于 2019-12-08 18:28:27
This is part of a larger statement, but I'm wondering if CTE or another method would help make this more efficient or cleaner. I could write it as a table-valued function and include it in my from clause, but I'd like to avoid extra objects if there is another solution. The SELECT TOP 1 ... sub-queries here simply catch when I have a rate with an earlier effective date than the base table, but I'm not fond of repeating them for each column I need to access. Is there a better way to accomplish this, or is this a normal looking statement? SELECT j.EmployeeId ,j.CompanyId ,j.JobCode ,COALESCE(j

How to reduce many similar correlated subqueries?

眉间皱痕 提交于 2019-12-08 07:12:52
问题 This is part of a larger statement, but I'm wondering if CTE or another method would help make this more efficient or cleaner. I could write it as a table-valued function and include it in my from clause, but I'd like to avoid extra objects if there is another solution. The SELECT TOP 1 ... sub-queries here simply catch when I have a rate with an earlier effective date than the base table, but I'm not fond of repeating them for each column I need to access. Is there a better way to accomplish

Force outer SELECT to fail if the inner SELECT contains an invalid identifier

混江龙づ霸主 提交于 2019-12-08 06:05:17
问题 If SELECT ID FROM T2 fails with the following message: Error: ORA-00904: "ID": invalid identifier why doesn't SELECT * FROM T1 WHERE ID IN ( SELECT ID FROM T2 ) fail ? (it returns all entries from T1) Is it possible to change this default behavior? (running the same query, but getting an error instead of all rows) I have: T1 with ID as column T2 with ID2 as column (T2 doesn't contain ID) but let's say that I use SELECT ID FROM T2 (see the example above) instead of SELECT ID2 FROM T2 by

Can we make a DISTINCT of a group_concat(distinct somefield)?

我只是一个虾纸丫 提交于 2019-12-08 05:11:40
问题 http://sqlfiddle.com/#!2/37dd94/17 If I do SELECT DISTINCT I get the same results as doing just SELECT . On the query results, you will see two activities that contains the District "Evora". Only one should appear. Any clue? 回答1: How about the following query (SQL FIDDLE): SELECT GROUP_CONCAT(APA_T.district), t.name FROM tbl_activity AS t JOIN tbl_activity_package AS ap ON t.id = ap.id_activity JOIN ( SELECT DISTINCT apa.district AS district, ( SELECT s1.id_activity_package FROM tbl_activity

Can I use table-valued function as query source in NHibernate?

左心房为你撑大大i 提交于 2019-12-07 07:40:17
问题 I have one question to ask you, dear community, as you may have guessed. So. I want NHibernate to filter the results of a query basing on the evaluation of table-valued sql function. Possible SQL query generated by NHibernate may look similar to the following: SELECT [whatever] FROM [whatever] INNER JOIN dbo.FnMyTableValuedFunction() as MyAlias ON [whatever].FirstDesiredKey = MyAlias.FirstDesiredKey AND [whatever].SecondDesiredKey = MyAlias.SecondDesiredKey Or it can be written this way:

How to optimize huge query with repeated subqueries

寵の児 提交于 2019-12-06 12:25:41
I have the following huge query that contains repeated subqueries , It looks really inefficient to me. How can i optimize it ? SELECT T2.date1, T2.date2, T2.period, T1.market, T1.ticker, 0 AS scenario FROM (SELECT DISTINCT Q.market AS market, Q.ticker AS ticker FROM portfolio.scenario S RIGHT JOIN portfolio.quote Q ON S.series = (SELECT S.series FROM scenario S WHERE S.date1 >= '2009-09-01' AND S.date2 <= '2010-07-01' AND S.period = 'QUARTER' ORDER BY S.date2 LIMIT 1) AND Q.market = S.market AND Q.ticker = S.ticker WHERE Q.date = '2010-07-01' AND S.date1 IS NULL) AS T1 JOIN (SELECT DISTINCT S

NHibernate correlated subquery using ICriteria

做~自己de王妃 提交于 2019-12-06 10:48:33
问题 I've been doing some work evaluating NHibernate for an upcoming project and am working through some use cases to see how it performs. I haven't yet been able to find a way to express the following query using the Criteri API. Two fairly basic tables (cut down for the purpose of this example) CREATE TABLE Person ( PersonNo INT, BirthDate DATETIME ) CREATE TABLE PersonDetails ( PersonNo INT, FirstName VARCHAR(30), Surname VARCHAR(30) ) And the query... SELECT P.PersonNo, P.FirstName, P.Surname

Can I use table-valued function as query source in NHibernate?

我们两清 提交于 2019-12-05 16:35:32
I have one question to ask you, dear community, as you may have guessed. So. I want NHibernate to filter the results of a query basing on the evaluation of table-valued sql function. Possible SQL query generated by NHibernate may look similar to the following: SELECT [whatever] FROM [whatever] INNER JOIN dbo.FnMyTableValuedFunction() as MyAlias ON [whatever].FirstDesiredKey = MyAlias.FirstDesiredKey AND [whatever].SecondDesiredKey = MyAlias.SecondDesiredKey Or it can be written this way: SELECT [whatever] FROM [whatever] WHERE EXISTS( SELECT 1 FROM dbo.FnMyTableValuedFunction() AS MyAlias