A.B.
A.B.

Reputation: 2470

SQL Join 2 tables without a relation

Let's say I have the following tables:

Table1 with cols: A, B
Table2 with col: C
And I have a variable @d.

|Table1|             |Table2|            @d = 5;
 ------               ------
 |A | B|             |   C  |
 -------              -------
 a1 | b1                 c1
 a2 | b2                 c2

How can I display following output?

|  ResultTable   |
------------------
|A  |  B |  C  | d|
 a1   b1    c1   5
 a2   b2    c2   5

PS: I am using T-SQL.

Upvotes: 0

Views: 157

Answers (1)

FuzzyTree
FuzzyTree

Reputation: 32402

You can use row_number to give each row a number and join on that. This assumes both tables have the same # of rows.

select *, @d from (
    select *, row_number() over (order by A) rn
    from Table1
) t1 join ( 
    select *, row_number() over (order by C) rn
    from Table2
) t2 on t1.rn = t2.rn

Upvotes: 3

Related Questions