subquery

Unknown column while using alias mysql

梦想的初衷 提交于 2020-01-14 06:17:10
问题 I am following SQL course from lagunita.satnford.edu . I am doing an exercise on practising queries, i have three tables: Movie ( mID, title, year, director ) Reviewer ( rID, name ) Rating ( rID, mID, stars, ratingDate ) Problem Statement: Find the difference between the average rating of movies released before 1980 and the average rating of movies released after 1980. (Make sure to calculate the average rating for each movie, then the average of those averages for movies before 1980 and

mysql passing data to in

别说谁变了你拦得住时间么 提交于 2020-01-14 04:29:07
问题 Basically I want to fetch some data from tblvw1. Another table contains possible ids within a column which is stored as concatenated string, for example: "1|2|3|4". Next I have tried to get the result by the following query: SELECT x FROM tblname1 WHERE id IN (SELECT REPLACE(content,'|',',') FROM tblname2 WHERE dataid = y ) AS result I only get the first value, the other data wasn't fetched. I suppose that the subquery will result in this form: SELECT x FROM tblname1 WHERE id IN ('1,2,3,4')

How to optimize huge query with repeated subqueries

时光毁灭记忆、已成空白 提交于 2020-01-13 19:30:10
问题 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

TSQL - TOP X in FROM Subquery?

坚强是说给别人听的谎言 提交于 2020-01-13 19:05:48
问题 Can someone please enlighten me to a way to filter a subquery that is located in a FROM clause? I would like it to look something like this: SELECT * FROM TABLE_A LEFT JOIN (TOP 8 TABLE_B) ON TABLE_B.id = TABLE_A.id 回答1: Please try this: SELECT column_names FROM TABLE_A A LEFT JOIN (SELECT TOP 8 column_names FROM TABLE_B) as B on A.Id=B.ID Considerations: Do not use * since it would lead to performance constraints. IF you are concerned about just the ID then get only the ID from Table_B HTH

Dynamically select the columns to be used in a SELECT statement

可紊 提交于 2020-01-13 09:01:53
问题 I would love to be able to use the system tables (Oracle in this case) to drive which fields are used in a SELECT statement. Something like: SELECT ( select column_name from all_tab_cols where table_Name='CLARITY_SER' AND OWNER='CLARITY' AND data_type='DATE' ) FROM CLARITY_SER This syntax doesn't work, as the subquery returns multiple rows, instead of one row with multiple columns. Is it possible to generate a SQL statement dynamically by querying the table schema information in order to

Doctrine2 Order By before Group By

流过昼夜 提交于 2020-01-13 06:15:13
问题 I am having issues implementing a sub-select solution for ORDERING a resulting dataset before the GROUP BY reduces it. Normally, in SQL you would do a sub-select: SELECT * FROM ( SELECT * FROM a_table order by a_table.timestamp desc ) as table_tmp group by userId However, I am having difficulty implementing this in DQL. Can anyone point me in the right direction please? My query is more complex than this and I assume I JOIN other tables through 'table_tmp' and in the outer SELECT. Thanks. 回答1

subset of data.frame columns to maximize “complete” observations

自作多情 提交于 2020-01-13 05:17:27
问题 I have a data frame with on the order of 20 numeric columns, each containing significant amounts of NA values. I would like to select a subset of these columns that will give me the most rows containing zero NA values. An exhaustive search would take a lot of computing time--is there a better way to get an approximation? Here is an example with a smaller data frame (completely arbitrary): set.seed(2) foo = as.data.frame(matrix(rnorm(200), nr = 20)) foo[sapply(foo, function(x) x > abs(x[1]))]

Counting rows from a subquery

烂漫一生 提交于 2020-01-12 12:04:26
问题 How could I count rows from a SELECT query as a value? Such as SELECT FUCNTIONIMLOOKINGFOR(SELECT * FROM anothertable) AS count FROM table; So that count is an integer of how many rows the subquery SELECT * FROM anothertable returns. EDIT SELECT p.PostPID, p.PostUID, p.PostText, p.PostTime, u.UserUID, u.UserName, u.UserImage, u.UserRep, ( SELECT COUNT(f.FlagTime) FROM Flags as f JOIN Posts as p ON p.PostPID = f.FlagPID ) as PostFlags FROM Posts AS p JOIN Users AS u ON p.PostUID = u.UserUID

Print Prime Numbers with SQL query

橙三吉。 提交于 2020-01-12 07:51:46
问题 I am new to StackOverflow and have got stuck with a query to print prime numbers from 2 to 1000. I have used the below query need input if this is the most efficient way to code it. WITH NUM AS ( SELECT LEVEL N FROM DUAL CONNECT BY LEVEL <= 1000 ) SELECT LISTAGG(B.N,'-') WITHIN GROUP(ORDER BY B.N) AS PRIMES FROM ( SELECT N, CASE WHEN EXISTS ( SELECT NULL FROM NUM N_INNER WHERE N_INNER .N > 1 AND N_INNER.N < NUM.N AND MOD(NUM.N, N_INNER.N)=0 ) THEN 'NO PRIME' ELSE 'PRIME' END IS_PRIME FROM NUM

Doctrine 2 subquery

霸气de小男生 提交于 2020-01-12 05:29:48
问题 I want to implement a subquery using the query builder but I'm not understanding the syntax. I'm dealing with a locations table that has entries that can be cities, states or zip codes depending on the location type set. I want to get all locations that are in a certain state and take out any that are city type and have a population below a certain amount. $qb->select('l') ->from('Entity\Location', 'l') ->where('l.state = :state') ->setParameter('state', 'UT') ->andWhere('...don't know what