self-join

n:m self-join with ruby on rails active record

被刻印的时光 ゝ 提交于 2019-12-25 12:55:23
问题 I'm trying to accomplish the following with ruby on rails, and I have problems to get the model-configuration right: I would like to model possible connections between airports. I created the following models: > rails generate model airport city name > rails generate model connection airport_id destination_id But most of the n:m association examples deal with associations between two different models on the one hand, or self-referencing models on the other hand, but I want to model a n:m

SQL Column compare in the same table (self-join)

北城以北 提交于 2019-12-25 07:25:45
问题 I need a hint in order to solve this SQL (self-join) problem: a table, with columns value and category id || value || category || foo ------------------------------------ 1 || 1 || a || 1 2 || 2 || a || 4 3 || 3 || a || 2 4 || 0 || b || 2 5 || 1 || b || 1 6 || 2 || b || 4 7 || 3 || b || 2 8 || 4 || b || 2 9 || 5 || b || 1 10 || 5 || b || 4 11 || 6 || b || 2 12 || 99 || z || 2 I would like to compare all values from category b and all values from category a and get all values that are in b and

How to Output the results of a MySQL query that used aliases?

你说的曾经没有我的故事 提交于 2019-12-24 21:19:52
问题 I have two primary MySQL tables (profiles and contacts) with many supplementary tables (prefixed by prm_). They are accessed and manipulated via PHP. In this instance I am querying the profiles table where I will retrieve an Owner ID and a Breeder ID. This will then be referenced against the contacts table where the information on the Owners and Breeders is kept. I received great help here on another question regarding joins and aliases, where I was also furnished with the following query.

mysql recursive self join

房东的猫 提交于 2019-12-24 09:13:23
问题 create table test( container varchar(1), contained varchar(1) ); insert into test values('X','A'); insert into test values('X','B'); insert into test values('X','C'); insert into test values('Y','D'); insert into test values('Y','E'); insert into test values('Y','F'); insert into test values('A','P'); insert into test values('P','Q'); insert into test values('Q','R'); insert into test values('R','Y'); insert into test values('Y','X'); select * from test; mysql> select * from test; +----------

MYSQL SQL (self) join?

旧巷老猫 提交于 2019-12-24 08:07:59
问题 Using this sample dataset: CREATE TABLE test. test2 (id VARCHAR(7), AA INT, BBB INT, CCC VARCHAR (12)); INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'A123', 45, 123, '2011-03' ); INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'A120', 52, 120, '2011-03' ); INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'A133', 63, 133, '2011-03' ); INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'D123', 34, 123, '2011-04' ); INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'D120' ,32, 120, '2011

non equi self join by group in data table

喜欢而已 提交于 2019-12-24 06:26:24
问题 Suppose I have the data: dat = data.table(A=c(1,2,3,1,4,5,1,2,3),B=c(1,1,1,NA,1,NA,2,NA,2), C=c(1,12,24.2,251,2,1,2,3,-1),D=c(1,1,1,1,1,2,2,2,2)) Which looks like: > dat A B C D 1: 1 1 1.0 1 2: 2 1 12.0 1 3: 3 1 24.2 1 4: 1 NA 251.0 1 5: 4 1 2.0 1 6: 5 NA 1.0 2 7: 1 2 2.0 2 8: 2 NA 3.0 2 9: 3 2 -1.0 2 And I want the desired output to, by group D, track the cumulative sum of A for whichever element occurs in B. So the output should be: > dat A B C D cumsum 1: 1 1 1.0 1 1 2: 2 1 12.0 1 1 3: 3 1

non equi self join by group in data table

一笑奈何 提交于 2019-12-24 06:26:14
问题 Suppose I have the data: dat = data.table(A=c(1,2,3,1,4,5,1,2,3),B=c(1,1,1,NA,1,NA,2,NA,2), C=c(1,12,24.2,251,2,1,2,3,-1),D=c(1,1,1,1,1,2,2,2,2)) Which looks like: > dat A B C D 1: 1 1 1.0 1 2: 2 1 12.0 1 3: 3 1 24.2 1 4: 1 NA 251.0 1 5: 4 1 2.0 1 6: 5 NA 1.0 2 7: 1 2 2.0 2 8: 2 NA 3.0 2 9: 3 2 -1.0 2 And I want the desired output to, by group D, track the cumulative sum of A for whichever element occurs in B. So the output should be: > dat A B C D cumsum 1: 1 1 1.0 1 1 2: 2 1 12.0 1 1 3: 3 1

Optimize MySQL self-join query

陌路散爱 提交于 2019-12-24 05:19:33
问题 I have c_regs table that contains duplicate rows. I've created index on form_number and property_name columns. Unfortunately this query still taking to-o-o-o long to complete, especially with addition of t10 and t11 joins. Is there a way to optimize it? Thanks. select ifnull(x.form_datetime,'') reg_date, ifnull(x.property_value,'') amg_id, x.form_number, x.form_name, x.form_version, ifnull(t1.property_value,'') first_name, ifnull(t2.property_value,'') last_name, ifnull(t3.property_value,'')

Not quite understanding the query after just shifting column names

白昼怎懂夜的黑 提交于 2019-12-24 00:58:37
问题 This is my table tblEmployee ,when I'm trying to use a self join query it's result is varying while shifting columns, can someone please explain me the difference between those two queries.Thanks EmployeeID Name ManagerID 1 Mike 3 2 Rob 1 3 Todd NULL 5 Ben 1 6 Sam 1 Query_1 SELECT E.Name AS Employee, M.Name AS Manager from tblEmployee E LEFT JOIN tblEmployee M on E.ManagerID = M.EmployeeID OUTPUT Employee Manager Mike Todd Rob Mike Todd NULL Ben Mike Sam Mike QUERY_2 SELECT E.Name AS Employee

Need some serious help with self join issue

拜拜、爱过 提交于 2019-12-23 18:18:44
问题 Well as you may know, you cannot index a view with a self join. Well actually even two joins of the same table, even if it's not technically a self join. A couple of guys from microsoft came up with a work around. But it's so complicated I don't understand it!!! The solution to the problem is here: http://jmkehayias.blogspot.com/2008/12/creating-indexed-view-with-self-join.html The view I want to apply this work around to is: create VIEW vw_lookup_test WITH SCHEMABINDING AS select count_big(*