self-join

SQL: self join using each rows only once [duplicate]

ぐ巨炮叔叔 提交于 2019-12-04 18:55:04
问题 This question already has an answer here : Closed 8 years ago . Possible Duplicate: combinations (not permutations) from cross join in sql I've currently got a table with the following records: A1 A2 A3 B1 B2 C1 C2 Where the same letter denotes some criteria in common (e.g. a common value for the column 'letter'). I do a self join on the criteria as follows: SELECT mytable.*, self.* FROM mytable INNER JOIN mytable AS self ON (mytable.letter = self.letter and mytable.number != self.number);

Mysql Self Join to find a parent child relationship in the same table

喜夏-厌秋 提交于 2019-12-04 15:33:30
Im trying to calculate the amount of money won by all the offspring of a male race horse (Sire) over a time period. Listed by the Sire with the most amount of money won. I run the query and get the result Im after with one problem, I cant display the sires name, only their ID. SELECT `horses`.`SireID` AS `SireID` , `horses`.`HorseName` AS `Sire Name`, COUNT( `runs`.`HorsesID` ) AS `Runs` , COUNT( CASE WHEN `runs`.`Finish` =1 THEN 1 ELSE NULL END ) AS `Wins` , CONCAT( FORMAT( ( COUNT( CASE WHEN `runs`.`Finish` =1 THEN 1 ELSE NULL END ) / COUNT ( `runs`.`TrainersID` ) ) *100, 0 ) , '%' ) AS

Oracle Self-Join on multiple possible column matches - CONNECT BY?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 13:45:49
I have a query requirement from ----. Trying to solve it with CONNECT BY , but can't seem to get the results I need. Table (simplified): create table CSS.USER_DESC ( USER_ID VARCHAR2(30) not null, NEW_USER_ID VARCHAR2(30), GLOBAL_HR_ID CHAR(8) ) -- USER_ID is the primary key -- NEW_USER_ID is a self-referencing key -- GLOBAL_HR_ID is an ID field from another system There are two sources of user data (datafeeds)... I have to watch for mistakes in either of them when updating information. Scenarios: A user is given a new User ID... The old record is set accordingly and deactivated (typically a

Odd behaviour of data.table's update on non-equi self-join

元气小坏坏 提交于 2019-12-04 11:28:00
While preparing an answer to the question dplyr or data.table to calculate time series aggregations in R I noticed that I do get different results depending on whether the table is updated in place or returned as a new object. Also, I do get different result when I change the order of columns in the non-equi join conditions. Currently, I don't have an explanation for this, perhaps due to a major misunderstanding on my side or a simple coding error. Please, note that this question is asking particularly for explanations of the observed behaviour of data.table joins. If you have alternative

“recursive” self join in data.table

倖福魔咒の 提交于 2019-12-04 11:16:54
I have a component list made of 3 columns: product, component and quantity of component used: a <- structure(list(prodName = c("prod1", "prod1", "prod2", "prod3", "prod3", "int1", "int1", "int2", "int2"), component = c("a", "int1", "b", "b", "int2", "a", "b", "int1", "d"), qty = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L)), row.names = c(NA, -9L), class = c("data.table", "data.frame")) prodName component qty 1 prod1 a 1 2 prod1 int1 2 3 prod2 b 3 4 prod3 b 4 5 prod3 int2 5 6 int1 a 6 7 int1 b 7 8 int2 int1 8 9 int2 d 9 Products with names starting with prod are final products, those with names like

How to implement self-join/cross-product with hadoop?

好久不见. 提交于 2019-12-04 10:18:39
It is common task to make some evaluation on pairs of items: Examples: de-duplication, collaborative filtering, similar items etc This is basically self-join or cross-product with the same source of data. To do a self join, you can follow the "reduce-side join" pattern. The mapper emits the join/foreign key as key, and the record as the value. So, let's say we wanted to do a self-join on "city" (the middle column) on the following data: don,baltimore,12 jerry,boston,19 bob,baltimore,99 cameron,baltimore,13 james,seattle,1 peter,seattle,2 The mapper would emit the key->value pairs: (baltimore -

how do I add a foreign key pointing to the same table using phpMyAdmin?

拜拜、爱过 提交于 2019-12-04 04:26:24
I have an existing InnoDB table which already has foreign keys pointing to different tables. But when I try to create a foreign key pointing to the Primary index, I get an error (check data type). The table is User with User_Id as the Primary. I want a foreign key Manager_ID which is a FK to User_Id. Both of INT Both of Length 10 Unsigned... But I still get a data check error...? Powerlord Make sure that Manager_ID is not set to NOT NULL . You have to allow nulls on that field, as the top-most person in the company will have no manager. I found a post over on the MySQL boards that might help.

Join table on itself - performance

本小妞迷上赌 提交于 2019-12-03 20:49:49
I would like some help with the following join. I have one table (with about 20 million rows) that consists of: MemberId (Primary Key) | Id (Primary Key) | TransactionDate | Balance I would like to get the latest Balance for all the customers in one query. I know I could do something like this (I just wrote it from my memory). But this way is terribly slow. SELECT * FROM money WHERE money.Id = (SELECT MAX(Id) FROM money AS m WHERE m.MemberId = money.MemberId) Are there any other (faster/smarter) options? In all optimization tutorials and screencasts that I've endured through, joins are always

SQL: self join using each rows only once [duplicate]

…衆ロ難τιáo~ 提交于 2019-12-03 12:36:18
Possible Duplicate: combinations (not permutations) from cross join in sql I've currently got a table with the following records: A1 A2 A3 B1 B2 C1 C2 Where the same letter denotes some criteria in common (e.g. a common value for the column 'letter'). I do a self join on the criteria as follows: SELECT mytable.*, self.* FROM mytable INNER JOIN mytable AS self ON (mytable.letter = self.letter and mytable.number != self.number); This join gives something like the following: A1 A2 A2 A1 A1 A3 A3 A1 A2 A3 A3 A2 B1 B2 B2 B1 C1 C2 C2 C1 However, I only want to include each pair once (a combination

First observation by group using self-join

青春壹個敷衍的年華 提交于 2019-12-03 02:56:23
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/fast-ways-in-r-to-get-the-first-row-of-a-data-frame-grouped-by-an-identifier I'm trying to implement