I created this sql DEMO :
I have #tbl1 :
___cola__
1
2
3
4
and #tbl2 :
_colb_
a
b
c
d
I want this :
_colb____|__cola____
a 1
b 2
c 3
d 4
I have found a solution ( bad IMHO)
SELECT table1.cola, table2.colb FROM
(SELECT cola, ROW_NUMBER() OVER (ORDER BY cola) AS rn1 FROM #tbl1) table1,
(SELECT colb, ROW_NUMBER() OVER (ORDER BY colb) AS rn2 FROM #tbl2) table2
WHERE table1.rn1 = table2.rn2
For knowledge ,
How ELSE can I do it ?
I started with :
SELECT cola , s.f FROM #tbl1 cross apply(select colb as f from #tbl2) s
But there is a problem at the right section.
Here’s an alternative:
And another:
But to be honest, the 2nd one is just rearranging the parts of the query – the execution plan is exactly the same as what you had. It works, and is the most efficient plan for such a
zip-upquery.