cross-join

SQL Server 2008 using SUM() OVER(ORDER BY…)

只愿长相守 提交于 2019-11-26 22:06:58
问题 I am trying to use a CTE and CROSS JOIN the result set. I want to sum up the 4 rows leading up to the current row. The example online I found does not use a CTE, only a newly created table (http://sqlandme.com/2011/08/17/sql-server-denali-over-rows-range/). The syntax should work, but I get an error saying "Incorrect syntax near 'ROWS'". An Example output would be this using the following statement: SUM(y) OVER(ORDER BY x ROWS 4 PRECEDING) sum X Y SUM 1 7 0 No prev rows, so sum is 0 2 1 7 Sum

How to generate a matrix of combinations

老子叫甜甜 提交于 2019-11-26 20:37:01
I have 5 items each of which can take on the value of 1 or -1. I want to generate a matrix that consists of rows of the possible combinations. The order of the items does not matter and the order of the combinations does not matter. I know I could do this mechanically, but I thought that someone must know a shortcut to generating this matrix. I apologize if this is similar to other questions but none of the solutions I have found can be applied to this particular problem with my programming skills. expand.grid(c(-1,1), c(-1,1), c(-1,1), c(-1,1), c(-1,1)) To generalize Greg's answer: N <- 5 vec

How do you perform a CROSS JOIN with LINQ to SQL?

ε祈祈猫儿з 提交于 2019-11-26 20:18:17
How do you perform a CROSS JOIN with LINQ to SQL? A cross-join is simply the Cartesian product of two sets. There's no explicit join operator for it. var combo = from p in people from c in cars select new { p.Name, c.Make, c.Model, c.Colour }; Rzv.im The same thing with linq extension methods: var names = new string[] { "Ana", "Raz", "John" }; var numbers = new int[] { 1, 2, 3 }; var newList=names.SelectMany( x => numbers, (y, z) => { return y + z + " test "; }); foreach (var item in newList) { Console.WriteLine(item); } Based on Steve's answer, the simplest expression would be this: var combo

combinations (not permutations) from cross join in sql

我与影子孤独终老i 提交于 2019-11-26 14:24:07
问题 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

How to do cross join in R?

杀马特。学长 韩版系。学妹 提交于 2019-11-26 12:24:17
How can I achieve a cross join in R ? I know that "merge" can do inner join, outer join. But I do not know how to achieve a cross join in R. Thanks Is it just all=TRUE ? x<-data.frame(id1=c("a","b","c"),vals1=1:3) y<-data.frame(id2=c("d","e","f"),vals2=4:6) merge(x,y,all=TRUE) From documentation of merge : If by or both by.x and by.y are of length 0 (a length zero vector or NULL), the result, r, is the Cartesian product of x and y, i.e., dim(r) = c(nrow(x)*nrow(y), ncol(x) + ncol(y)). If speed is an issue, I suggest checking out the excellent data.table package. In the example at the end it's

Mixing implicit and explicit JOINs

别来无恙 提交于 2019-11-26 09:55:59
问题 I am having a problem with Hibernate generating invalid SQL. Specifically, mixing and matching implicit and explicit joins. This seems to be an open bug. However, I\'m not sure why this is invalid SQL. I have come up with a small toy example that generates the same syntax exception. Schema CREATE TABLE Employee ( employeeID INT, name VARCHAR(255), managerEmployeeID INT ) Data INSERT INTO Employee (employeeID, name) VALUES (1, \'Gary\') INSERT INTO Employee (employeeID, name, managerEmployeeID

SQL Server: What is the difference between CROSS JOIN and FULL OUTER JOIN?

廉价感情. 提交于 2019-11-26 08:47:52
问题 What is the difference between CROSS JOIN and FULL OUTER JOIN in SQL Server? Are they the same, or not? Please explain. When would one use either of these? 回答1: A cross join produces a cartesian product between the two tables, returning all possible combinations of all rows. It has no on clause because you're just joining everything to everything. A full outer join is a combination of a left outer and right outer join. It returns all rows in both tables that match the query's where clause,

How do you perform a CROSS JOIN with LINQ to SQL?

天大地大妈咪最大 提交于 2019-11-26 07:32:55
问题 How do you perform a CROSS JOIN with LINQ to SQL? 回答1: A cross-join is simply the Cartesian product of two sets. There's no explicit join operator for it. var combo = from p in people from c in cars select new { p.Name, c.Make, c.Model, c.Colour }; 回答2: The same thing with linq extension methods: var names = new string[] { "Ana", "Raz", "John" }; var numbers = new int[] { 1, 2, 3 }; var newList=names.SelectMany( x => numbers, (y, z) => { return y + z + " test "; }); foreach (var item in

What is the expected behaviour for multiple set-returning functions in SELECT clause?

邮差的信 提交于 2019-11-26 06:47:16
问题 I\'m trying to get a \"cross join\" with the result of two set-returning functions, but in some cases I don\'t get the \"cross join\", see example Behaviour 1 : When set lenghts are the same, it matches item by item from each set postgres=# SELECT generate_series(1,3), generate_series(5,7) order by 1,2; generate_series | generate_series -----------------+----------------- 1 | 5 2 | 6 3 | 7 (3 rows) Behaviour 2 : When set lenghts are different, it \"cross join\"s the sets postgres=# SELECT

How to do cross join in R?

别说谁变了你拦得住时间么 提交于 2019-11-26 01:53:46
问题 How can I achieve a cross join in R ? I know that \"merge\" can do inner join, outer join. But I do not know how to achieve a cross join in R. Thanks 回答1: Is it just all=TRUE ? x<-data.frame(id1=c("a","b","c"),vals1=1:3) y<-data.frame(id2=c("d","e","f"),vals2=4:6) merge(x,y,all=TRUE) From documentation of merge : If by or both by.x and by.y are of length 0 (a length zero vector or NULL), the result, r, is the Cartesian product of x and y, i.e., dim(r) = c(nrow(x)*nrow(y), ncol(x) + ncol(y)).