correlated-subquery

MySQL - alternatives to nested subqueries when limiting aggregate data in a correlated subquery

左心房为你撑大大i 提交于 2019-12-04 04:07:01
问题 I have a table that looks something like this: DataTable +------------+------------+------------+ | Date | DailyData1 | DailyData2 | +------------+------------+------------+ | 2012-01-23 | 146.30 | 212.45 | | 2012-01-20 | 554.62 | 539.11 | | 2012-01-19 | 710.69 | 536.35 | +------------+------------+------------+ I'm trying to create a view (call it AggregateView ) that will, for each date and for each data column, show a few different aggregates. For example, select * from AggregateView where

ERROR: subquery in FROM cannot refer to other relations of same query level

江枫思渺然 提交于 2019-12-03 13:50:29
问题 I'm working with PostgreSQL 9 and I want to find the nearest neighbor inside table RP for all tuples in RQ , comparing the dates ( t ), but I get this error: ERROR: subquery in FROM cannot refer to other relations of same query level using this query: SELECT * FROM RQ, (SELECT * FROM RP ORDER BY ABS(RP.t - RQ.t) LIMIT 1) AS RA RQ.t in subquery seems to be the problem. How can I avoid this error? How can I get access from subquery to RQ ? 回答1: Update: LATERAL joins allow that and were

ERROR: subquery in FROM cannot refer to other relations of same query level

我们两清 提交于 2019-12-03 03:52:49
I'm working with PostgreSQL 9 and I want to find the nearest neighbor inside table RP for all tuples in RQ , comparing the dates ( t ), but I get this error: ERROR: subquery in FROM cannot refer to other relations of same query level using this query: SELECT * FROM RQ, (SELECT * FROM RP ORDER BY ABS(RP.t - RQ.t) LIMIT 1) AS RA RQ.t in subquery seems to be the problem. How can I avoid this error? How can I get access from subquery to RQ ? Erwin Brandstetter Update: LATERAL joins allow that and were introduced with Postgres 9.3. Details: What is the difference between LATERAL and a subquery in

MySQL LIMIT in a Correllated Subquery

a 夏天 提交于 2019-12-02 07:11:40
I have a correlated subquery that will return a list of quantities, but I need the highest quantity, and only the highest. So I tried to introduce an order by and a LIMIT of 1 to achieve this, but MySQL throws an error stating it doesn't yet support limits in subqueries. Any thoughts on how to work around this? SELECT Product.Name, ProductOption.Name, a.Qty, a.Price, SheetSize.UpgradeCost, FinishType.Name, FinishOption.Name, FinishTierPrice.Qty, FinishTierPrice.Price FROM `Product` JOIN `ProductOption` ON Product.idProduct = ProductOption.Product_idProduct JOIN `ProductOptionTier` AS a ON a

MySQL LIMIT in a Correllated Subquery

て烟熏妆下的殇ゞ 提交于 2019-12-02 06:48:36
问题 I have a correlated subquery that will return a list of quantities, but I need the highest quantity, and only the highest. So I tried to introduce an order by and a LIMIT of 1 to achieve this, but MySQL throws an error stating it doesn't yet support limits in subqueries. Any thoughts on how to work around this? SELECT Product.Name, ProductOption.Name, a.Qty, a.Price, SheetSize.UpgradeCost, FinishType.Name, FinishOption.Name, FinishTierPrice.Qty, FinishTierPrice.Price FROM `Product` JOIN

MySQL - alternatives to nested subqueries when limiting aggregate data in a correlated subquery

坚强是说给别人听的谎言 提交于 2019-12-01 23:32:32
I have a table that looks something like this: DataTable +------------+------------+------------+ | Date | DailyData1 | DailyData2 | +------------+------------+------------+ | 2012-01-23 | 146.30 | 212.45 | | 2012-01-20 | 554.62 | 539.11 | | 2012-01-19 | 710.69 | 536.35 | +------------+------------+------------+ I'm trying to create a view (call it AggregateView ) that will, for each date and for each data column, show a few different aggregates. For example, select * from AggregateView where Date = '2012-01-23' might give: +------------+--------------+----------------+--------------+---------

Get count of foreign key from multiple tables

纵然是瞬间 提交于 2019-12-01 17:59:11
问题 I have 3 tables, with Table B & C referencing Table A via Foreign Key. I want to write a query in PostgreSQL to get all ids from A and also their total occurrences from B & C. a | b | c ----------------------------------- id | txt | id | a_id | id | a_id ---+---- | ---+----- | ---+------ 1 | a | 1 | 1 | 1 | 3 2 | b | 2 | 1 | 2 | 4 3 | c | 3 | 3 | 3 | 4 4 | d | 4 | 4 | 4 | 4 Output desired (just the id from A & total count in B & C) : id | Count ---+------- 1 | 2 -- twice in B 2 | 0 -- occurs

SQL Delete clears the table instead of erroring

☆樱花仙子☆ 提交于 2019-12-01 06:34:32
I have a piece of SQL which (you would think) wouldn't compile, but which instead deletes all rows from the target table. Consider this setup: create table TableA (ColumnA varchar(200)); create table TableB (ColumnB varchar(200)); insert TableA values ('A'),('B'),('C'); insert TableB values ('A'); Then the following sql: --Returns all rows from TableA select * from TableA; --Does not error (ColumnA does not exist on TableB) delete TableA where ColumnA in (select ColumnA from TableB) --No Rows are returned select * from TableA; The delete statement above causes all rows to be removed from

SQL - Relationship between a SubQuery and an Outer Table

不打扰是莪最后的温柔 提交于 2019-11-30 22:38:26
问题 Problem I need to better understand the rules about when I can reference an outer table in a subquery and when (and why) that is an inappropriate request. I've discovered a duplication in an Oracle SQL query I'm trying to refactor but I'm running into issues when I try and turn my referenced table into a grouped subQuery. The following statement works appropriately: SELECT t1.* FROM table1 t1, INNER JOIN table2 t2 on t1.id = t2.id and t2.date = (SELECT max(date) FROM table2 WHERE id = t1.id)

Count of unique values in a rolling date range for R

冷暖自知 提交于 2019-11-30 15:42:29
问题 This question already has an answer for SQL, and I was able to implement that solution in R using sqldf . However, I've been unable to find a way to implement it using data.table . The problem is to count the distinct values of one column within a rolling date range, e.g. (and quoting directly from the linked question) if the data looked like this: Date | email -------+---------------- 1/1/12 | test@test.com 1/1/12 | test1@test.com 1/1/12 | test2@test.com 1/2/12 | test1@test.com 1/2/12 |