cross-join

mysql cross join, but without duplicated pair?

 ̄綄美尐妖づ 提交于 2019-12-04 06:25:25
Let's say I have the following row in my table table rows id 63 64 65 66 67 68 if I run the following query, I get 30 rows. SELECT r1.id, r2,id FROM rows AS r1 CROSS JOIN rows AS r2 WHERE r1.id!=r2.id result: 63 64 65 64 66 64 67 64 68 64 64 63 65 63 66 63 67 63 68 63 63 65 64 65 66 65 67 65 68 65 63 66 64 66 65 66 67 66 68 66 63 67 64 67 65 67 66 67 68 67 63 68 64 68 65 68 66 68 67 68 how would I get the following result instead of the above? 63,64 63,65 63,66 63,67 63,68 64,65 64,66 64,67 64,68 65,66 65,67 65,68 66,67 66,68 67,68 as you see, I don't want to get both 63,64 and 64,63, for

Is CROSS JOIN a synonym for INNER JOIN without ON clause?

一世执手 提交于 2019-12-03 14:38:45
问题 I am wondering whether CROSS JOIN can be safely replaced with INNER JOIN in any query when it is found. Is an INNER JOIN without ON or USING exactly the same as CROSS JOIN ? If yes, has the CROSS JOIN type been invented only to express intent better in a query? An appendix to this question would be: Can there be a difference using modern and widely used DBMSes when using CROSS JOIN ... WHERE x , INNER JOIN ... ON ( x ) or INNER JOIN ... WHERE ( x ) ? Thank you. 回答1: In all modern databases

Add missing rows to data.table according to multiple keyed columns

◇◆丶佛笑我妖孽 提交于 2019-12-03 14:03:11
I have a data.table object that contains multiple columns that specify unique cases. In the small example below, the variables " name ", " job ", and " sex " specify the unique IDs. I would like to add missing rows so that each each case has a row for each possible instance of another variable, " from " (similar to expand.grid ). library(data.table) set.seed(1) mydata <- data.table(name = c("john","john","john","john","mary","chris","chris","chris"), job = c("teacher","teacher","teacher","teacher","police","lawyer","lawyer","doctor"), sex = c("male","male","male","male","female","female","male

In SQL, what's the difference between JOIN and CROSS JOIN?

為{幸葍}努か 提交于 2019-12-03 13:19:30
问题 What's the difference between: select t1.a1, t1.a2, t1.a3 from t1 cross join t2 where t1.a3 = t2.a1 and: select t1.a1, t1.a2, t1.a3 from t1,t2 where t1.a3=t2.a1; Can I use them interchangeably? 回答1: MySQL doesn't offer a distinction between JOIN and CROSS JOIN . They are the same. In both your examples the clause WHERE t1.a3 = t2.a1 converts any sort of join into an inner join. The standard way of expressing this query is SELECT t1.a1, t1.a2, t1.a3 FROM t1 JOIN t2 ON t1.a3 = t2.a1 回答2: SQL

Is CROSS JOIN a synonym for INNER JOIN without ON clause?

三世轮回 提交于 2019-12-03 04:27:42
I am wondering whether CROSS JOIN can be safely replaced with INNER JOIN in any query when it is found. Is an INNER JOIN without ON or USING exactly the same as CROSS JOIN ? If yes, has the CROSS JOIN type been invented only to express intent better in a query? An appendix to this question would be: Can there be a difference using modern and widely used DBMSes when using CROSS JOIN ... WHERE x , INNER JOIN ... ON ( x ) or INNER JOIN ... WHERE ( x ) ? Thank you. In all modern databases all these constructs are optimized to the same plan. Some databases (like SQL Server ) require an ON condition

spark cross join,two similar code,one works,one not

 ̄綄美尐妖づ 提交于 2019-12-02 10:05:41
I have a following code: val ori0 = Seq( (0l, "1") ).toDF("id", "col1") val date0 = Seq( (0l, "1") ).toDF("id", "date") val joinExpression = $"col1" === $"date" ori0.join(date0, joinExpression).show() val ori = spark.range(1).withColumn("col1", lit("1")) val date = spark.range(1).withColumn("date", lit("1")) ori.join(date,joinExpression).show() The first join works,but the second has an error: Exception in thread "main" org.apache.spark.sql.AnalysisException: Detected implicit cartesian product for INNER join between logical plans Range (0, 1, step=1, splits=Some(4)) and Project [_1#11L AS id

Trying to calculate quartiles in MDX

心不动则不痛 提交于 2019-12-02 08:28:50
My data looks like this: ID |PersonID |CompanyID |DateID |Throughput |AmountType 33F467AC-F35B-4F24-A05B-FC35CF005981 |7 |53 |200802 |3 |0 04EE0FF0-511D-48F5-AA58-7600B3A69695 |18 |4 |201309 |5 |0 AB058AA5-6228-4E7C-9469-55827A5A34C3 |25 |69 |201108 |266 |0 with around a million rows. The columns names *ID refers to other tables, so they can be used as dimensions. I have an OLAP cube with the column Throughput as Measure and the rest as dimensions. I want to calculate Quartile 1 and 3 of the Throughput measure. I followed this guide: https://electrovoid.wordpress.com/2011/06/24/ssas-quartile/

Summarize the self-join index while avoiding cartesian product in R data.table

眉间皱痕 提交于 2019-12-01 09:47:36
With a 2-column data.table , I'd like to summarize the pairwise relationships in column 1 by summing the number of shared elements in column 2. In other words, how many shared Y elements does each pairwise combination of X-values have? For example, I can do this in a 2-step process, first doing a cartesian cross join, then summarizing it like so: d = data.table(X=c(1,1,1,2,2,2,2,3,3,3,4,4), Y=c(1,2,3,1,2,3,4,1,5,6,4,5)) setkey(d, Y) d2 = d[d, allow.cartesian=TRUE] d2[, .N, by=c("X", "i.X")] # X i.X N #1: 1 1 3 #2: 2 1 3 #3: 3 1 1 #4: 1 2 3 #5: 2 2 4 #6: 3 2 1 #7: 1 3 1 #8: 2 3 1 #9: 3 3 3 #10:

Aggregate functions on multiple joined tables

梦想的初衷 提交于 2019-12-01 08:26:32
问题 I have three tables: CREATE TABLE foo ( id bigint PRIMARY KEY, name text NOT NULL ); CREATE TABLE foo_bar ( id bigint PRIMARY KEY, foo_id bigint NOT NULL ); CREATE TABLE tag ( name text NOT NULL, target_id bigint NOT NULL, PRIMARY KEY (name, target_id) ); I'm trying to create a view such that I get all of the fields of table foo , the count of items in foo_bar where foo.id = foo_bar.foo_id , and a text array of all tags where foo.id = tag.target_id . If we have: INSERT INTO foo VALUES (1,

error : subquery must return only one column

◇◆丶佛笑我妖孽 提交于 2019-12-01 03:20:27
I am getting the error subquery must return only one column when I try to run the following query: SELECT mat.mat as mat1, sum(stx.total ) as sumtotal1, ( SELECT mat.mat as mat, sum(stx.total) as sumtotal FROM stx LEFT JOIN mat ON stx.matid = mat.matid LEFT JOIN sale ON stx.saleid = sale.id WHERE stx.date BETWEEN '2013-05-01' AND '2013-08-31' AND sale.userid LIKE 'A%' GROUP BY mat.mat ) AS MyField FROM stx LEFT JOIN mat ON stx.matid = mat.matid LEFT JOIN sale ON stx.saleid = sale.id WHERE stx.date BETWEEN '2013-05-01' AND '2013-08-31' AND sale.userid LIKE 'B%' GROUP BY mat.mat What is causing