subquery

Error when calculating a running total (cumulative over the previous periods)

一曲冷凌霜 提交于 2019-12-10 23:16:10
问题 I have a table, let's call it My_Table that has a Created datetime column (in SQL Server) that I'm trying to pull a report that shows historically how many rows were to My_Table by month over a particular time. Now I know that I can show how many were added each month with: SELECT YEAR(MT.Created), MONTH(MT.Created), COUNT(*) AS [Total Added] FROM My_Table MT GROUP BY YEAR(MT.Created), MONTH(MT.Created) ORDER BY YEAR(MT.Created), MONTH(MT.Created) Which would return something like: YEAR MONTH

SQL JOIN To Find Records That Don't Have a Matching Record With a Specific Value

﹥>﹥吖頭↗ 提交于 2019-12-10 19:35:17
问题 I'm trying to speed up some code that I wrote years ago for my employer's purchase authorization app. Basically I have a SLOW subquery that I'd like to replace with a JOIN (if it's faster). When the director logs into the application he sees a list of purchase requests he has yet to authorize or deny. That list is generated with the following query: SELECT * FROM SA_ORDER WHERE ORDER_ID NOT IN (SELECT ORDER_ID FROM SA_SIGNATURES WHERE TYPE = 'administrative director'); There are only about

mySQL: Subquery to array?

夙愿已清 提交于 2019-12-10 18:38:20
问题 I am working on a slight complex (at least for me) mySQL query containing a subquery and it isn't going to well to be honest. SELECT `products`.`id`, `product`.`price`, ( SELECT `value` FROM (`productValues`) WHERE `productValues`.`product` = 'product.id' ) as values FROM (`products`) WHERE`product`.`active` = 1 The current result looks like this: Array ( [0] => Array ( [id] => 1 [active] => 1 [price] => 1000 [values] => ) ) What I want is the values element to also become an array with all

How to get a nested sub-query to recognize parent query column

房东的猫 提交于 2019-12-10 18:30:59
问题 I have a problem where I'm trying to calculate a sum of values per given id. I decided to do this using sub-queries (typically I'd use a join, but I'm also keeping a counter for each sub-query for clipping purposes - see this question for more info). For the sake of this question, assume I have the following MySQL query: /* 1. */ SELECT /* 2. */ t1.experiement_id, /* 3. */ (SELECT sum(x.size) /* 4. */ FROM (SELECT size, ( @rownum := @rownum + 1 ) AS `rownum` /* 5. */ FROM data AS t0 /* 6. */

How to limit query results to exact group match

最后都变了- 提交于 2019-12-10 18:07:50
问题 I have a table like the following: user | item ------------- X | Apple X | Orange X | Pear Y | Orange Y | Pear Z | Apple Z | Orange My goal is to have 3 search options: ANY , ALL (At Least), EXACT Where ANY returns a list of users who have at least one item searched for, so searching for "Apple" - ANY would return X,Z, searching for "Apple, Orange" - ANY would return X,Y,Z ALL returns a list of users who have all items searched for, so searching for "Apple" - ALL would return X,Z, searching

Subqueries vs Inner joins - Which one executes faster?

牧云@^-^@ 提交于 2019-12-10 17:54:21
问题 I hope it's ok to make a posting like this. I have been using SQL for quite some time and people at work have been using 2 different ways to return the same number or rows in a database. For example: SELECT Name FROM Employees WHERE DepartmentID IN (SELECT DepartmentID FROM Departments WHERE Department LIKE '%Engineering') SELECT Employees.Name FROM Departments INNER JOIN Employees ON Departments.DepartmentID = Employees.DepartmentID WHERE Departments.Department LIKE '%Engineering' Both

Why is this (non-correlated) subquery causing such problems?

廉价感情. 提交于 2019-12-10 17:35:39
问题 I've got a large query where a simple subquery optimization dropped it from 8 minutes down to 20 seconds. I'm not sure I understand why the optimization had such a drastic effect. In essence, here's the problem part: SELECT (bunch of stuff) FROM a LEFT OUTER JOIN b ON a.ID = b.a LEFT OUTER JOIN c ON b.ID = c.b ... ... INNER JOIN veryLargeTable ON a.ID = veryLargeTable.a AND veryLargeTable.PetID = (SELECT id from Pets WHERE Pets.Name = 'Something') /* BAD! */ ... ... In all, there are 16

SQL NOT IN possibly performance issues

怎甘沉沦 提交于 2019-12-10 17:31:36
问题 I am attempting to refactor several old pieces of code... I have refactored the current piece below and have highlighted the NOT IN statement causing performance issues. I am attempting to rewrite the NOT IN section with a left outer join. Can anyone help, or suggest a better way if possible? SELECT left(unique_id,16) AS casino_id , right(unique_id,24) AS game_id FROM ( SELECT distinct o.casino_id + g.game_id AS unique_id FROM game g INNER JOIN Bet b ON g.game_id = b.game_id INNER JOIN

create new table with subquery select

元气小坏坏 提交于 2019-12-10 16:49:51
问题 I'm trying to create a new table from a subquery select but I get the following error: Incorrect syntax near ')'. SELECT * INTO foo FROM ( SELECT DATEPART(MONTH,a.InvoiceDate) as CalMonth ,DATEPART(YEAR,a.InvoiceDate) as CalYear ,a.InvoiceDate ,a.StockCode ,a.QtyInvoiced ,a.Volume FROM sales a UNION ALL SELECT ds.CalMonth as CalMonth ,ds.CalYear as CalYear ,ds.InvoiceDate ,ds.StockCode ,ds.Cases as QtyInvoiced ,ds.Vol as Volume FROM sales1 ds ) 回答1: You forgot to add alias at the end of your

NSPredicate with SubQuery

◇◆丶佛笑我妖孽 提交于 2019-12-10 15:43:16
问题 I have this relationship: player <—>> games <<—> quiz and want to get all quiz not in a game of a player, like SELECT * FROM ZQUIZ WHERE Z_PK NOT IN (SELECT ZQUIZ FROM ZGAME WHERE ZPLAYER == 1) Can anybody help? 回答1: This can be done with a SUBQUERY clause. If myPlayer is the player in question: let predicate = NSPredicate(format:"SUBQUERY(games,$g, $g.player == %@).@count == 0", myPlayer) 来源: https://stackoverflow.com/questions/35688892/nspredicate-with-subquery