subquery

Rewriting a slow SQL (sub) query in JOIN

主宰稳场 提交于 2019-12-25 03:02:50
问题 So I've got massive slow SQL query and I've narrowed it down to a slow sub-query, so I want to rewrite it to a JOIN. But I'm stuck... (due to the MAX and GROUP BY ) SELECT * FROM local.advice AS aa LEFT JOIN webdb.account AS oa ON oa.shortname = aa.shortname WHERE aa.aa_id = ANY (SELECT MAX(dup.aa_id) FROM local.advice AS dup GROUP BY dup.shortname) AND oa.cat LIKE '111' ORDER BY aa.ram, aa.cpu DESC LIMIT 0, 30 回答1: Here is a different version of your query where the subquery is converted

fetching names from one table with multiple ids in join

痞子三分冷 提交于 2019-12-25 02:21:49
问题 Employee ID Name 1 John 2 Williams Appointment ID DoctorName Employee_id Team lead 1 willson 1 2 SQL SELECT E.*,A.* FROM Employee as E,Appointment as A WHERE A.Employee_id = E.ID This is my join if i want to fetch Team lead name how we can do that? 回答1: Try this one SELECT E.name FROM Employee E JOIN Appointment A ON A.Employee_id = E.ID WHERE [some condition here] 回答2: IF Team Lead NAME IN Appointment TABLE THEN USE below QUERY: SELECT E.Name AS 'Employee Name',A.DoctorName AS 'Doctor', A

JPQL query not working after needed to adapt it

删除回忆录丶 提交于 2019-12-25 01:49:34
问题 I needed to change my JPQL query because it turned out that the last element of types ( contains 5 elements ) contains a wildcard so it can itself contain many elements basically. So now I changed the original query to only check if the TestcaseEntity contains 4 types instead of 5 and all the testcases coming out of this query I check now if they have any of the typesVariants ( previous 5 element ) which should deliver the resulting testcases. So now I wrote a subselect . What I found out now

Convert SQL Query with Subquery to Laravel query

↘锁芯ラ 提交于 2019-12-25 01:44:08
问题 Is it possible to convert my SQL Query to Laravel 4 Query SQL: SELECT Branch_tbl.ID, Branch_tbl.BranchName, ( SELECT SUM(Expenses.Expense) FROM Expenses WHERE Expenses.BranchID = Branch_tbl.ID ) as 'Total Expenses' FROM Branch_tbl 回答1: You may try raw expression (maybe it's not to best solution) DB::table('branch_tbl') ->select( 'branch_tbl.id', 'branch_tbl.branchname', DB::raw(" ( select sum(expenses.expense) from expenses where expenses.branchid = branch_tbl.id )as 'total expenses'"))->get(

MySQL: delete rows returned by subquery

…衆ロ難τιáo~ 提交于 2019-12-25 01:17:59
问题 I need to remove rows where there is a duplicate link column from a table. I'm trying to run: delete from resultitem where id in (select r.id from resultitem r group by r.link having count(r.id) >1); But getting an error: ERROR 1093 (HY000): You can't specify target table 'resultitem' for update in FROM clause Is this possible to remove rows by subquery in MySQL without a temporary table? Please advise. 回答1: This should delete all but the lowest id per link : delete ri1 from resultitem as ri1

Join 2 subqueries

独自空忆成欢 提交于 2019-12-25 01:11:58
问题 I would appreciate any assistance w/this. I'm trying to perform a join on 2 subqueries, but it keeps saying the 'IntEncTracking.EncounterList.ClaimId' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause, but I've tried qualifying it with an el and q2, to no avail. Any advice would be greatly appreciated. Declare @SweepId as int = 10160, @RunType as varchar = 'Initial' Select * from (Select distinct ClaimId , LineNum , 0 as

Select Subquery Group By

南笙酒味 提交于 2019-12-25 00:33:40
问题 I am having an issue with this simple query. I get the error "Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <=, >, >= or when the subquery is used as an expression." SELECT TOP 100 PERCENT dbo.Inventory.PARTNO ,( SELECT ISNULL(SUM(dbo.PurchaseOrderReceived.QtyReceived), 0) FROM dbo.PurchaseOrderReceived JOIN dbo.PurchaseOrderlineItems ON dbo.PurchaseOrderReceived.POLIID = dbo.PurchaseOrderlineItems.POLIID JOIN dbo.Inventory ON dbo

Calculate Final outcome based on Results/ID

北慕城南 提交于 2019-12-24 23:15:19
问题 For a Table T1 +----------+-----------+-----------------+ | PersonID | Date | Employment | +----------+-----------+-----------------+ | 1 | 2/28/2017 | Stayed the same | | 1 | 4/21/2017 | Stayed the same | | 1 | 5/18/2017 | Stayed the same | | 2 | 3/7/2017 | Improved | | 2 | 4/1/2017 | Stayed the same | | 2 | 6/1/2017 | Stayed the same | | 3 | 3/28/2016 | Improved | | 3 | 5/4/2016 | Improved | | 3 | 4/19/2017 | Worsened | | 4 | 5/19/2016 | Worsened | | 4 | 2/16/2017 | Improved | +----------+-

Implementing a WHERE clause within a subquery for the value of a pivot column?

夙愿已清 提交于 2019-12-24 20:16:59
问题 I've successfully managed to perform a few table joins and generate a result using a pivot table (with the aid of Ollie Jones). The sql statement and result are below. I'd like the results returned to only have rows with values of pivot column 'date' (I think that's the terminology!?) set for today or in the future. From what I can see WHERE date >= CURDATE() should do the job however because 'date' doesn't technically exist I receive an error on execution when added to the WHERE clause at

Can I reorder SQL selections after the limit has been applied?

穿精又带淫゛_ 提交于 2019-12-24 17:19:20
问题 I'd like to see the last ten rows from our mysql database in ID order. Naively, I'd expect to be able to do something like this: SELECT * FROM ( SELECT * FROM things ORDER BY id DESC LIMIT 10) ORDER BY id ASC but that isn't valid syntax. What's the correct way of expressing the query I'm trying to run? 回答1: You got that almost right: SELECT * FROM ( SELECT * FROM things ORDER BY id DESC LIMIT 10) xxx ORDER BY id ASC Note the innocent xxx after the subselect which you need. 回答2: Try: SELECT *