self-join

Rails: How do self-referential has_many models work?

…衆ロ難τιáo~ 提交于 2019-11-28 20:42:01
So, I'm working on an app where I want to users to be able to group objects in "folders". Basically: User has_many :foos Foos don't have to be in a folder, but they can be. In that case: Folder has_many :foos and Foo belongs_to :folder Now, I'd like to be able to set up folders so they can be nested. I think this is something like... Folder has_many :folders I have heard that this kind of self-referential relationship is no big deal, but I don't really get how it works. I haven't been able to figure out how this is supposed to be declared in the model and what columns I need to provide in the

Entity Framework 4.1 Code First Self-Referencing One-to-Many and Many-to-Many Associations

女生的网名这么多〃 提交于 2019-11-28 17:57:47
I have a User that can have collection of users he likes... Another user can have collection of users he likes.... If User A likes User B and if User B likes User A, then they get to hang out. I need to send each other their contact info. How do we represent such a model in Entity Framework Code First? public class User { public int UserId { get; set; } public int? UserLikeId { get; set; } public virtual UserLike UserLike { get; set; } } public class UserLike { public int UserLikeId { get; set; } public int UserId { get; set; } public virtual User User { get; set; } public virtual ICollection

Multiple Self-Join based on GROUP BY results

时光总嘲笑我的痴心妄想 提交于 2019-11-28 02:19:00
I'm attempting to collect details about backup activity from a ProgreSQL DB table on a backup appliance (Avamar). The table has several columns including: client_name, dataset, plugin_name, type, completed_ts, status_code, bytes_modified and more. Simplified example: | session_id | client_name | dataset | plugin_name | type | completed_ts | status_code | bytes_modified | |------------|-------------|---------|---------------------|------------------|----------------------|-------------|----------------| | 1 | server01 | Windows | Windows File System | Scheduled Backup | 2017-12-05T01:00:00Z |

First observation by group using self-join

强颜欢笑 提交于 2019-11-27 14:42:18
问题 I'm trying to get the top row by a group of three variables using a data.table. I have a working solution: col1 <- c(1,1,1,1,2,2,2,2,3,3,3,3) col2 <- c(2000,2000,2001,2001,2000,2000,2001,2001,2000,2000,2001,2001) col4 <- c(1,2,3,4,5,6,7,8,9,10,11,12) data <- data.frame(store=col1,year=col2,month=12,sales=col4) solution1 <- data.table(data)[,.SD[1,],by="store,year,month"] I used the slower approach suggested by Matthew Dowle in the following link: https://stats.stackexchange.com/questions/7884

recursive self query

懵懂的女人 提交于 2019-11-27 12:56:18
I have the following table: myTable: +----+----------+ | id | parentID | +----+----------+ | 1 | null | | 2 | 1 | | 3 | 1 | | 4 | 2 | | 5 | 4 | ----------------- i would like to get all rows tracing back until there's no parentID anymore. So ".... WHERE id=5" would give me: 5, 4, 2, 1 You are organizing your hierarchical data using the adjacency list model . The fact that such recursive operations are difficult is in fact one major drawback of this model. Some DBMSes, such as SQL Server 2005, Postgres 8.4 and Oracle 11g, support recursive queries using common table expressions with the WITH

Entity Framework 4.1 Code First Self-Referencing One-to-Many and Many-to-Many Associations

南楼画角 提交于 2019-11-27 10:57:57
问题 I have a User that can have collection of users he likes... Another user can have collection of users he likes.... If User A likes User B and if User B likes User A, then they get to hang out. I need to send each other their contact info. How do we represent such a model in Entity Framework Code First? public class User { public int UserId { get; set; } public int? UserLikeId { get; set; } public virtual UserLike UserLike { get; set; } } public class UserLike { public int UserLikeId { get;

Join two tables (with a 1-M relationship) where the second table needs to be 'flattened' into one row

喜你入骨 提交于 2019-11-27 09:08:13
Given the following tables: Student +----+-------+ | id | Name | +----+-------+ | 1 | Chris | | 2 | Joe | | 3 | Jack | +----+-------+ Enrollment +---------------+------------+-----------+----------+ | enrollment_id | student_id | course_id | complete | +---------------+------------+-----------+----------+ | 1 | 1 | 55 | true | | 2 | 1 | 66 | true | | 3 | 1 | 77 | true | | 4 | 2 | 55 | true | | 5 | 2 | 66 | false | | 6 | 3 | 55 | false | | 7 | 3 | 66 | true | +---------------+------------+-----------+----------+ I would like the following +----+-------+-----------+-----------+-----------+ | id

combinations (not permutations) from cross join in sql

谁说胖子不能爱 提交于 2019-11-27 08:54:04
If I have a table that I'd like to cross join to itself, how can I remove the duplicate rows? Or to put it another way, how can I do a "order doesn't matter" cross join? So for example, if I have a table T: field | ------- A | B | C | and I cross join to itself so that i don't get the A | A rows T as t1 cross join T as t2 on t1.field != t2.field I would get the following: field | field ------+------- A | B A | C B | A B | C C | A C | B However, to me A, B is the same as B, A. Is there a good way to remove these duplicates? In other words, I want the combinations not the permutations. T as t1

MySql. How to use Self Join

强颜欢笑 提交于 2019-11-27 03:24:45
问题 I need to use Self Join on this table. +------------+------+--------+ | Country | Rank | Year | +------------+------+--------+ |France | 55 | 2000 | +------------+------+--------+ |Canada | 30 | 2000 | +------------+------+--------+ |Liberia | 59 | 2001 | +------------+------+--------+ |Turkey | 78 | 2000 | +------------+------+--------+ |Japan | 65 | 2003 | +------------+------+--------+ |Romania | 107 | 2001 | +------------+------+--------+ I need to use self join to get what countries has

Rails: self join scheme with has_and_belongs_to_many?

走远了吗. 提交于 2019-11-27 02:04:27
I would like to create a structure of Users having many friends , also of class User : class User < ActiveRecord::Base has_and_belongs_to_many :friends, class_name: "User" end I do not need any details of their relationship thus I do not use :through with kind of class Friendship . But now I cannot find any way how to create corresponding database (neither with migration file nor using rails g model User username:string ... command). Any ideas? Paul Richter Here are some resources which may be helpful: RailsCasts episode #163 Self-Referential Association regarding self-referential many-to-many