I have two tables, let's call them t1
and t2
. They're associated – column c1
in t2
matches the primary key column i1
in t1
.
For my results, I want every row in t1
to appear without any t2
data; then, in the following output rows, I want to see the combined data from the t1
row and any matching t2
rows. Is that possible in MS SQL? How can I do it?
Table t1: Table t2:
+----+----+ +----+----+----+
| i1 | x | | i2 | c1 | c2 |
+----+----+ +----+----+----+
| 1 | a | | 1 | 1 | |
| 2 | b | | 2 | 2 | g |
| 3 | c | | 3 | 3 | h |
| 4 | d | | 4 | 3 | i |
+----+----+ +----+----+----+
Result
+----+----+----+---+----+----+
| i | i1 | i2 | x |c1 | c2 |
+----+----+----+---+----+----+
| 1 | 1 |NULL| a |NULL|NULL|<-- `t1` First Row
| 2 | 1 | 1 | a | 1 |NULL|
| 3 | 2 |NULL| b |NULL|NULL|<-- `t1` Second Row
| 4 | 2 | 2 | b | 2 | g |
| 5 | 3 |NULL| c |NULL|NULL|<-- `t1` Third Row
| 6 | 3 | 3 | c | 3 | h |
| 7 | 3 | 4 | c | 3 | i |
| 8 | 4 |NULL| d |NULL|NULL|<-- `t1` Fourth Row
+----+----+----+---+----+----+
NOTE: column i
in the output is simply a row number.
Best Answer
Your question is a little confusing (in particular around the joins), does this look something like what you are looking for?