correlated-subquery

SQL how to subtract result row 1 from row 2, row 2 from row 3

徘徊边缘 提交于 2021-02-08 08:35:56
问题 How do I subtract row 1 from row 2 and row 2 from row 3, etc. in MySQL? The table i am pulling my data from contains multiple products and all products have multiple prices (on different dates) The code i am working with: SELECT orderline_sales.product_name, orderline_sales.price FROM orderline_sales GROUP BY price HAVING orderline_sales.product_name = 'Ibuprofen'; The result I am getting: |---------------------|------------------| | product_name | price | |---------------------|-------------

mySQL correlated Subquery

徘徊边缘 提交于 2021-02-07 19:56:33
问题 trying to write a mysql query and having a lot of difficult with this one. I have two tables( Item: info about items, and itemReview: reviews for the items ) What I would like to do is select all the items that belong to a particular location (which is what my outer query does) and then for each item in the outer query, get the average of all the rating fields in the itemReview table Here is my attempt: SELECT Item.idDish, Item.dateAdded, Item.dateModified, Item.fidLocation, Item.category,

Select distinct values with count in PostgreSQL

匆匆过客 提交于 2020-08-26 19:54:54
问题 This is a heavily simplified version of an SQL problem I'm dealing with. Let's say I've got a table of all the cities in the world, like this: country city ------------ Canada Montreal Cuba Havanna China Beijing Canada Victoria China Macau I want to count how many cities each country has, so that I would end up with a table as such: country city_count ------------------ Canada 50 Cuba 10 China 200 I know that I can get the distinct country values with SELECT distinct country FROM T1 and I

Referencing Parent query inside Child query

 ̄綄美尐妖づ 提交于 2020-01-25 07:58:09
问题 Can anyone explain this query when subquery is referencing parent. How does SQL think about this Second highest salary of employee: select max(e1.sal),e1.deptno from s_emp e1 where sal < (select max(sal) from s_emp e2 where e2.deptno = e1.deptno) group by e1.deptno; I tested it and it works. 回答1: First remove group by and aggegation and consider this query: select e1.sal, e1.deptno from s_emp e1 where e1.sal < (select max(sal) from s_emp e2 where e2.deptno = e1.deptno) It returns all the rows

Select distinct users group by time range

青春壹個敷衍的年華 提交于 2020-01-24 09:30:50
问题 I have a table with the following info |date | user_id | week_beg | month_beg| SQL to create table with test values: CREATE TABLE uniques ( date DATE, user_id INT, week_beg DATE, month_beg DATE ) INSERT INTO uniques VALUES ('2013-01-01', 1, '2012-12-30', '2013-01-01') INSERT INTO uniques VALUES ('2013-01-03', 3, '2012-12-30', '2013-01-01') INSERT INTO uniques VALUES ('2013-01-06', 4, '2013-01-06', '2013-01-01') INSERT INTO uniques VALUES ('2013-01-07', 4, '2013-01-06', '2013-01-01') INPUT

spark sql error when running correlated subquery

血红的双手。 提交于 2020-01-15 09:39:25
问题 I'm running below spark SQL query in Intellij Maven IDE, SELECT seq_no, amount, (select max(b.amount) from premium b where b.seq_no < a.seq_no) last_high_prem FROM premium a Got the below error, Exception in thread "main" org.apache.spark.sql.AnalysisException: The correlated scalar subquery can only contain equality predicates: (seq_no#11#32 < seq_no#11); I understand correlated query when uses equality operator then only works currently in spark SQL. Is there any method to overcome this

Select parent and child from the same table

。_饼干妹妹 提交于 2020-01-14 06:03:49
问题 I have a emp table, CREATE TABLE [dbo].[Emp]( [EmpId] [int] NULL, [EmpName] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [ManagerId] [int] NULL ) ON [PRIMARY] Now, insert below values into the table Insert Into Emp Values(1,'A',0) Insert Into Emp Values(2,'B',1) Insert Into Emp Values(3,'C',2) Insert Into Emp Values(4,'D',2) Insert Into Emp Values(5,'E',4) Insert Into Emp Values(6,'F',4) Insert Into Emp Values(7,'G',4) Insert Into Emp Values(8,'H',6) Insert Into Emp Values(9,'I'

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

“invalid reference to FROM-clause entry for table” in Postgres query

时光毁灭记忆、已成空白 提交于 2020-01-03 20:12:09
问题 I have the following query: query = "SELECT data #>> '{id}' AS id, data #>> '{name}' AS name, data #>> '{curator}' AS curator, data #> '{$isValid}' AS \"$isValid\", data #> '{customer}' AS customer, data #> '{$createdTS}' AS \"$createdTS\", data #> '{$updatedTS}' AS \"$updatedTS\", data #> '{$isComplete}' AS \"$isComplete\", (count(keys))::numeric as \"numProducts\", created_at FROM appointment_intakes, LATERAL jsonb_object_keys(data #> '{products}') keys INNER JOIN appointment_intake_users

MySql scoping problem with correlated subqueries

左心房为你撑大大i 提交于 2019-12-28 16:02:22
问题 I'm having this Mysql query, It works: SELECT nom ,prenom ,(SELECT GROUP_CONCAT(category_en) FROM (SELECT DISTINCT category_en FROM categories c WHERE id IN (SELECT DISTINCT category_id FROM m3allems_to_categories m2c WHERE m3allem_id = 37) ) cS ) categories ,(SELECT GROUP_CONCAT(area_en) FROM (SELECT DISTINCT area_en FROM areas c WHERE id IN (SELECT DISTINCT area_id FROM m3allems_to_areas m2a WHERE m3allem_id = 37) ) aSq ) areas FROM m3allems m WHERE m.id = 37 The result is: nom prenom