join

SQL join to get the cartesian product of 2 columns out of 3 columns

孤者浪人 提交于 2021-02-10 03:18:03
问题 I have the following table: create table #table ( time int, key char(1), val int ) with the following data: insert into #table (time, key, val) values (0,"a",1) insert into #table (time, key, val) values (0,"b",2) insert into #table (time, key, val) values (1,"a",10) insert into #table (time, key, val) values (2,"b",20) and I would like to come up with a join of that will yield the following rows/cols: 0 a 1 0 b 2 1 a 10 1 b 0 2 a 0 2 b 20 Which is basically the cartesian product of the

SQL join to get the cartesian product of 2 columns out of 3 columns

◇◆丶佛笑我妖孽 提交于 2021-02-10 03:17:12
问题 I have the following table: create table #table ( time int, key char(1), val int ) with the following data: insert into #table (time, key, val) values (0,"a",1) insert into #table (time, key, val) values (0,"b",2) insert into #table (time, key, val) values (1,"a",10) insert into #table (time, key, val) values (2,"b",20) and I would like to come up with a join of that will yield the following rows/cols: 0 a 1 0 b 2 1 a 10 1 b 0 2 a 0 2 b 20 Which is basically the cartesian product of the

.SD and .SDcols for the i expression in data.table join

≯℡__Kan透↙ 提交于 2021-02-10 02:57:09
问题 i'm trying to copy a subset of columns from Y to X based on a join, where the subset of columns is dynamic I can identify the columns quite easily: names(Y)[grep("xxx", names(Y))] but when i try to use that code in the j expression, it just gives me the column names, not the values of the columns. the .SD and .SDcols gets pretty close, but they only apply to the x expression . I'm trying to do something like this: X[Y[names(Y)[grep("xxx", names(Y))] := .SD, .SDcols = names(Y)[grep("xxx",

.SD and .SDcols for the i expression in data.table join

别说谁变了你拦得住时间么 提交于 2021-02-10 02:56:06
问题 i'm trying to copy a subset of columns from Y to X based on a join, where the subset of columns is dynamic I can identify the columns quite easily: names(Y)[grep("xxx", names(Y))] but when i try to use that code in the j expression, it just gives me the column names, not the values of the columns. the .SD and .SDcols gets pretty close, but they only apply to the x expression . I'm trying to do something like this: X[Y[names(Y)[grep("xxx", names(Y))] := .SD, .SDcols = names(Y)[grep("xxx",

.SD and .SDcols for the i expression in data.table join

一笑奈何 提交于 2021-02-10 02:55:57
问题 i'm trying to copy a subset of columns from Y to X based on a join, where the subset of columns is dynamic I can identify the columns quite easily: names(Y)[grep("xxx", names(Y))] but when i try to use that code in the j expression, it just gives me the column names, not the values of the columns. the .SD and .SDcols gets pretty close, but they only apply to the x expression . I'm trying to do something like this: X[Y[names(Y)[grep("xxx", names(Y))] := .SD, .SDcols = names(Y)[grep("xxx",

MYSQL nested query running very slow?

淺唱寂寞╮ 提交于 2021-02-09 19:04:21
问题 The following query is constantly timing out, is there a less overhead way to achieve the same function ? UPDATE Invoices SET ispaid = 0 WHERE Invoice_number IN (SELECT invoice_number FROM payment_allocation WHERE transactionID=305) What I'm doing is unallocating invoices from a transaction, there can be up to 30+ records returned but it stops the database dead everytime I try to run it 回答1: USE JOIN instead of subquery it will improve the performance. Create index on Invoice_number column in

MYSQL nested query running very slow?

折月煮酒 提交于 2021-02-09 19:03:38
问题 The following query is constantly timing out, is there a less overhead way to achieve the same function ? UPDATE Invoices SET ispaid = 0 WHERE Invoice_number IN (SELECT invoice_number FROM payment_allocation WHERE transactionID=305) What I'm doing is unallocating invoices from a transaction, there can be up to 30+ records returned but it stops the database dead everytime I try to run it 回答1: USE JOIN instead of subquery it will improve the performance. Create index on Invoice_number column in

MYSQL nested query running very slow?

ぃ、小莉子 提交于 2021-02-09 18:58:09
问题 The following query is constantly timing out, is there a less overhead way to achieve the same function ? UPDATE Invoices SET ispaid = 0 WHERE Invoice_number IN (SELECT invoice_number FROM payment_allocation WHERE transactionID=305) What I'm doing is unallocating invoices from a transaction, there can be up to 30+ records returned but it stops the database dead everytime I try to run it 回答1: USE JOIN instead of subquery it will improve the performance. Create index on Invoice_number column in

How do I remove results based on conditions to calculate an average

烂漫一生 提交于 2021-02-09 11:44:11
问题 I have the schema below. A quick explanation of it is: bob rated up 5/5 james rated up 1/5 macy rated up 5/5 The logic: If I am personA, look up everyone I have blocked. Look up all the movie reviews. Anyone who has left a movie review, and personA has blocked, remove them from the calculation. Calculate the average rating of the movies. CREATE TABLE movies ( id integer AUTO_INCREMENT primary key, name varchar(100) NOT NULL ); CREATE TABLE customer ( id integer AUTO_INCREMENT primary key,

How do I remove results based on conditions to calculate an average

夙愿已清 提交于 2021-02-09 11:42:26
问题 I have the schema below. A quick explanation of it is: bob rated up 5/5 james rated up 1/5 macy rated up 5/5 The logic: If I am personA, look up everyone I have blocked. Look up all the movie reviews. Anyone who has left a movie review, and personA has blocked, remove them from the calculation. Calculate the average rating of the movies. CREATE TABLE movies ( id integer AUTO_INCREMENT primary key, name varchar(100) NOT NULL ); CREATE TABLE customer ( id integer AUTO_INCREMENT primary key,