correlated-subquery

Count of unique values in a rolling date range for R

人盡茶涼 提交于 2019-11-30 14:57:09
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 | test2@test.com 1/3/12 | test@test.com 1/4/12 | test@test.com 1/5/12 | test@test.com 1/5/12 | test@test.com

SQL join to correlated subquery where tables are related by overlapping ranges

不想你离开。 提交于 2019-11-29 16:01:35
I have the following table structure: Item ID | Name -------- 1 | Apple 2 | Pear 3 | Banana 4 | Plum 5 | Tomato Event ItemStart | ItemEnd | EventType | EventDate -------------------------------------------- 1 | 2 | Planted | 2014-01-01 1 | 3 | Picked | 2014-01-02 3 | 5 | Eaten | 2014-01-05 The two tables are linked only by the primary key of Item and the range of ItemStart and ItemEnd (inclusive) in Event. Events always refer to contiguous sequences of Items, but not all the Events for a given Item will have the same range. Events never occur on the same date for a given Item. The query I'd

Query with broken sub-select should result in error but returns rows

让人想犯罪 __ 提交于 2019-11-29 10:21:25
I don't understand the behaviour in this case. In my understanding a query with an invalid sub-query should result in an error. But in this example it returns some rows. Test-Data: create table test_values ( tst_id number, tst_id2 number, tst_value varchar2( 10 ) ); create table test_lookup ( tst_id number, tst_value varchar2( 10 ) ); insert into test_values( tst_id, tst_id2, tst_value ) values ( 1, 2, 'a' ); insert into test_values( tst_id, tst_id2, tst_value ) values ( 1, 2, 'b' ); insert into test_values( tst_id, tst_id2, tst_value ) values ( 2, 2,'c' ); insert into test_values( tst_id, tst

SQL Server “cannot perform an aggregate function on an expression containing an aggregate or a subquery”, but Sybase can

删除回忆录丶 提交于 2019-11-29 00:05:54
问题 This issue has been discussed before, but none of the answers address my specific problem because I am dealing with different where clauses in the inner and outer selects. This query executed just fine under Sybase, but gives the error in the title of this post when executed under SQL Server. The query is complicated, but the general outline of the query is: select sum ( t.graduates - ( select sum ( t1.graduates ) from table as t1 where t1.id = t.id and t1.group_code not in ('total', 'others'

MySql scoping problem with correlated subqueries

不问归期 提交于 2019-11-28 12:24:08
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 categories areas Man Multi Carpentry,Paint,Walls Beirut,Baalbak,Saida It works correclty, but only when i

JPA Criteria select all instances with max values in their groups

微笑、不失礼 提交于 2019-11-28 10:24:18
Is there a way to write with JPA 2 CriteriaBuilder the equivalent of the following query? select * from season s1 where end = ( select max(end) from season s2 where s1.contest_id=s2.contest_id ); In JPQL this query is: Select s1 from Season s1 where s1.end = ( select max(s2.end) from Season s2 where s1.contest=s2.contest ) This should work, with contest being either a basic Integer property, or a ManyToOne property pointing to another non-basic Entity. EntityManger em; //to be injected or constructed CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Season> cq = cb.createQuery(Season

SQL join to correlated subquery where tables are related by overlapping ranges

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 09:56:59
问题 I have the following table structure: Item ID | Name -------- 1 | Apple 2 | Pear 3 | Banana 4 | Plum 5 | Tomato Event ItemStart | ItemEnd | EventType | EventDate -------------------------------------------- 1 | 2 | Planted | 2014-01-01 1 | 3 | Picked | 2014-01-02 3 | 5 | Eaten | 2014-01-05 The two tables are linked only by the primary key of Item and the range of ItemStart and ItemEnd (inclusive) in Event. Events always refer to contiguous sequences of Items, but not all the Events for a

How to create a correlated update subquery in MS-Access?

时光总嘲笑我的痴心妄想 提交于 2019-11-28 06:42:41
问题 I'm in the process of normalizing a few tables and I've added a surrogate primary key to a table called Exams which holds exam titles. Previously, the child tables would just use the entire name of the exam as the FK entry. Now that I've added a autonumbered field to the table, I want to update the entries that use it such as the table where the questions are from as there are over a thousand of them. Going through each exam with a modified update once I find the name each time would take a

Query with broken sub-select should result in error but returns rows

青春壹個敷衍的年華 提交于 2019-11-28 03:38:06
问题 I don't understand the behaviour in this case. In my understanding a query with an invalid sub-query should result in an error. But in this example it returns some rows. Test-Data: create table test_values ( tst_id number, tst_id2 number, tst_value varchar2( 10 ) ); create table test_lookup ( tst_id number, tst_value varchar2( 10 ) ); insert into test_values( tst_id, tst_id2, tst_value ) values ( 1, 2, 'a' ); insert into test_values( tst_id, tst_id2, tst_value ) values ( 1, 2, 'b' ); insert

Generate sql with subquery as a column in select statement using SQLAlchemy

若如初见. 提交于 2019-11-27 06:47:36
问题 Is there a way to make SQLAlchemy generate a query with a custom column that is a subquery that correlates with current row: SELECT tab1.id, tab1.col1, ..., ( SELECT count(1) FROM tab2 WHERE tab2.tab1_id = tab1.id GROUP BY tab2.col1 ) as cnt FROM tab1 WHERE ... LIMIT 100 using the ORM API? session.query(Tab1, ?(subquery for additional column)?).filter(...).limit(100) I'm using PostgreSQL 9.3 and old version of SQLAlchemy 0.9.8 回答1: If you need this often, and/or the count is an integral part