subquery

MySQL query to Select -> Insert and Delete selected rows

浪子不回头ぞ 提交于 2019-12-11 05:27:22
问题 I got a system that pings to the database every 2 to 5 seconds, when an user is connected the application. Depending on his connection, the ping timeframe can be bigger, like 10 seconds or so. Example: Pings: 1,4,6,8,9,12,16,20,50,180,187,189,200,203,206,210 ... I'm running a query to grab ranges that does not exceed 1 minute between the pings, group them, so I can tell for how long the user has been connected: Here is the query I'm running to select the results, as advised by @fancyPants on

Column ambiguously defined in subquery using rownums

落花浮王杯 提交于 2019-12-11 05:26:53
问题 I have to execute a SQL made from some users and show its results. An example SQL could be this: SELECT t1.*, t2.* FROM table1 t1, table2 t2, where table1.id = table2.id This SQL works fine as it is, but I need to manually add pagination and show the rownum, so the SQL ends up like this. SELECT z.* FROM( SELECT y.*, ROWNUM rn FROM ( SELECT t1.*, t2.* FROM table1 t1, table2 t2, where table1.id = table2.id ) y WHERE ROWNUM <= 50) z WHERE rn > 0 This throws an exception: "ORA-00918: column

Sequelize condition on joined table doesn't work with limit condition

前提是你 提交于 2019-12-11 05:13:51
问题 I have a Supplier model with associated Calendar models. I want to fetch suppliers who either have a calendar which is set to available don't have a calendar I can do this using the following: Supplier.findAll({ include: [ { model: Calendar, as: 'calendars', required: false, where: { start_time: { [Op.lte]: date }, end_time: { [Op.gte]: date }, }, }, ], where: { '$calendars.state$': { [Op.or]: [ { [Op.in]: ['available'] }, { [Op.eq]: null }, ], }, }, }); This generates the following SQL

How to return two fields from a subquery

时光怂恿深爱的人放手 提交于 2019-12-11 05:05:53
问题 The query below works, but I would like to know if there is a better way to do it. There is one subquery that uses two subqueries. The two subqueries are identical but return two different fields. Is there a way to use only one subquery that returns two fields? I checked similar questions (this, this and this), but I don't think they apply in this case. Here is the query: SELECT *, time(strftime('%s', EndTime) - strftime('%s', StartTime), 'unixepoch') AS Duration FROM ( SELECT ( SELECT Time

MySQL complex subquery formulation

给你一囗甜甜゛ 提交于 2019-12-11 04:57:45
问题 I have two tables - books and images . books has columns like id , name , releasedate , purchasecount . images has bookid (which is same as the id in books, basically one book can have multiple images. Although I haven't set any foreign key constraint), bucketid , poster (each record points to an image file in a certain bucket, for a certain bookid ). Table schema: poster is unique in images , hence it is a primary key. Covering index on books: ( name , id , releasedate ) Covering index on

Calculating Percentage within MYSQL query based on conditions

两盒软妹~` 提交于 2019-12-11 04:43:11
问题 hope you can help, this is driving me up the wall I need to calculate the percentage of times a question has been failed, but this needs to be narrowed down by the geographical area, and product these questions are being asked against. I have : $CA002 = "( SELECT ROUND(100 * (SELECT count(CA002Result) from Data_Table where (CA002Result='Fail'))/count(CA002Result),2) from Data_Table) AS 'CA002 %'"; Which 'works' but just calculates against the whole set of records as an 'overall' I'm trying :

MySQLi performance, multiple (separate) queries vs subqueries

China☆狼群 提交于 2019-12-11 04:37:02
问题 I need to count the number of rows from different(!) tables and save the results for some kind of statistic. The script is quite simple and working as expected, but I'm wondering if it's better to use a single query with (in this case) 8 subqueries, or if I should use separate 8 queries or if there's even a better, faster and more advanced solution... I'm using MySQLi with prepared statements, so the single query could look like this: $sql = 'SELECT (SELECT COUNT(cat1_id) FROM `cat1`),

Using subquery for _PARTITIONTIME in bigquery does not limit cost

非 Y 不嫁゛ 提交于 2019-12-11 03:55:39
问题 When I run the below query on BQ using standard SQL, it says it will process 76.6TB when running SELECT event_time, user_id, activity_id,dbm_insertion_order_id, dbm_total_media_cost_usd FROM `raw.5295.activity_*` WHERE _PARTITIONTIME >(SELECT * FROM `analytics-dwh.autobidding.activity_list` ) AND timestamp_micros(event_time) > (SELECT timestamp_micros(MAX(event_time)) from `essence-analytics-dwh.ml_for_autobidding.nest_na_4q18_activity_updated_daily`) AND _TABLE_SUFFIX IN ('25','20') The

MySQL is extremely slow on EC2

喜你入骨 提交于 2019-12-11 03:46:48
问题 I have found out that MySQL on EC2 (Ubuntu 12.10) could be extremely slow. It takes just 700ms for a certain set of SQL queries to perform on my local PC (Windows 7), whereas on EC2 it requires more than 13sec. The database is very small, just 12MB. There is almost no disk IO during the query. Nevertheless, EC2 instance is 20 times slower. All the databases are based on the same dump: same tables and same indexes. The queries return the same results. The only difference is the execution time.

subquery - getting the highest score

╄→尐↘猪︶ㄣ 提交于 2019-12-11 03:45:37
问题 I am trying to get the student that scored highest on the final exam first I select SELECT s.STUDENT_ID, w.LAST_NAME,w.FIRST_NAME, MAX(s.NUMERIC_GRADE) AS NUMERIC_FINAL_GRADE FROM GRADE s , SECTION z, STUDENT w WHERE s.SECTION_ID = z.SECTION_ID AND s.STUDENT_ID = w.STUDENT_ID AND z.COURSE_NO = 230 AND z.SECTION_ID = 100 AND s.GRADE_TYPE_CODE = 'FI' GROUP BY s.STUDENT_ID, w.FIRST_NAME,w.LAST_NAME and it gives me this result and that is what I want STUDENT_ID LAST_NAME FIRST_NAME NUMERIC_FINAL