subquery

Are subqueries the only option to reuse variables?

孤街醉人 提交于 2019-12-25 04:36:16
问题 I'd like to use MySQL in this form: SELECT 1 AS one, one*2 AS two because it's shorter and sweeter than SELECT one*2 AS two FROM ( SELECT 1 AS one ) AS sub1 but the former doesn't seem to work because it expects one to be a column. Is there any easier way to accomplish this effect without subqueries? And no, SELECT 2 AS two is not an option. ;) 回答1: select @one := 1 as one, 2 * @one as two; user-defined variables 回答2: Considering this SQL code SELECT 1 AS one, one*2 AS two from the

SQL subqueries errors

依然范特西╮ 提交于 2019-12-25 04:24:52
问题 Using the AdventureWorks2012 database, I have to change the query below to select the top 10% orders with CustomerId greater than 500 and sequence the orders by TotalDue in descending sequence. SELECT * FROM Sales.SalesOrderHeader h INNER JOIN Sales.SalesOrderDetail d ON d.SalesOrderId = h.SalesOrderId Here are two of my attempts at solving the problem, but both contain errors: Msg 156, Level 15, State 1, Line 9 Incorrect syntax near the keyword 'DESC'. Msg 156, Level 15, State 1, Line 12

get max ids by group mysql

此生再无相见时 提交于 2019-12-25 04:14:34
问题 I have this table called sw_sowing ----------------------------------------------------------------- id | id_unit | date | id_variety | type | status | ----------------------------------------------------------------- 1 | 1 | 2017-08-10 | 1 | SW | 200 | ----------------------------------------------------------------- 2 | 1 | 2017-10-10 | 1 | ER | 100 | ----------------------------------------------------------------- 3 | 1 | 2017-11-30 | 2 | SW | 100 | ---------------------------------------

Failure To Execute SQL Subquery With Join

故事扮演 提交于 2019-12-25 04:12:09
问题 Please can someone tell me what I'm not doing right. Here is a query I want to execute, but nothing happens when I run the command. I'm new to SQL so pardon my mistake, if any. SELECT t.*, COUNT(DISTINCT t.subjects) AS subjectenrollment, u.urefnumber, u.uresidence FROM ( SELECT r.*, @curRank := IF( @prevRank = finalscore, @curRank, @incRank ) AS position, @incRank := @incRank + 1, @prevRank = finalscore FROM studentsreports r, ( SELECT @curRank := 0, @prevRank = NULL, @incRank := 1 ) c ORDER

Subquery in cakePHP to lookup in other table

自作多情 提交于 2019-12-25 03:54:54
问题 I recently started experimenting with CakePHP. I used this query in "normal" PHP and need to convert it to a CakePHP find. However I can't seem to make it work! SELECT *, (SELECT name FROM `users` WHERE `users`.`id`=`products`.`creator`) AS creatorname FROM `products` WHERE `products`.`ID`='2' It's a basic set up with 2 tables, users and products. Every product gets created by a user and I need to load the name of the user along with the product info. (So I can display the username and not

annual change of values with subquery

拥有回忆 提交于 2019-12-25 03:50:39
问题 This query gives a 1093 error: UPDATE values_table as outer_select SET annual_change = sample_value - ( SELECT sample_value FROM values_table WHERE date_sampled = DATE_SUB(outer_select.date_sampled, INTERVAL 1 YEAR) ); I'm trying to set annual_change for every row equal to the current row's sample_value less last year's sample_value . The data doesn't go back to the beginning time, so how can the lack of historical values also be handled? 回答1: Try this UPDATE values_table as a join values

Join table to a subquery

ぃ、小莉子 提交于 2019-12-25 03:22:53
问题 In a previous post someone helped me with a subquery. Now I'd like to add to the query but am getting an error message. (I'm still learning rules around subqueries.) Updated SQL below. The error is: [Amazon](500310) Invalid operation: invalid reference to FROM-clause entry for table "application_stages"; SELECT t1.*, applications.status, applications.stage_name FROM application_stages t1 JOIN ( select application_id, max(exited_on) as exited_on from application_stages group by application_id

Querying Relationship Existence, how to add conditions depending on an array length

江枫思渺然 提交于 2019-12-25 03:15:42
问题 I would like to filter an item (pubs, in this case) by some characteristics that are stored in a separate table (tapps, in this case), and both are related by pub_tapps. I have the following tables: pubs, tapps, pub_tapps(pub_id, tapp_id) The relation between Pub and Tapp is the following: public function pubTapps() { return $this->belongsToMany(Tapp::class, 'pub_tapps'); } in my Pub model I tried the following testing for an array $request=[5,8, 7]: public function pubsFilteredByTapps

Product with last 4 vendors on transaction date

瘦欲@ 提交于 2019-12-25 03:11:59
问题 Hi I have this sql and have to translate into NHibernate QueryOver SELECT S.Number, S.Description, S.BrandDescription, subquery.vendornumber, subquery.vendorname FROM Stocks.Stock S left join (select vendorNumber, VendorName, POLID, LastTransactionDate from ( SELECT top 4 v.Number vendorNumber, v.Name VendorName, PLL.Id POLID, max(por.TransactionDate) as LastTransactionDate, ROW_NUMBER() OVER(PARTITION BY v.Number ORDER BY max(por.TransactionDate) DESC) AS rk FROM Purchasing

MySQL moving average calculation

有些话、适合烂在心里 提交于 2019-12-25 03:04:55
问题 How can i edit the ON operator part of my query below such that i would like the current code to work where id<=14 (which is t2.id <= t1.id as shown below) so when t1 id =14, t2 is the cumulative id from 1 to 14 (as it is now). but for id >14 I would like the ON operator to be (t2.id=t1.id>=t1.id-2 and <=t1.id) so when t1 id=15, t2.id should be between 13 and 15. when t1 id =16, t2.id should be between 14 and 16 and so on. I'm doing this because when i calculate col E for ids after id=14, i