无需连接即可组合到表中

时间:2017-07-04 06:32:38

标签: sql sql-server

我有两张这两张桌子

表A

       Col1 Col2
        1     A
        5     B
        3     A

表B

 Column1 Column2
    11     Aa
    15     Bb

如何通过TableA.col1 desc和TableB.Column1 desc进行排序,从而获得此结果集

       Col1 Col2 Column1 Column2
        1     A    11     Aa
        3     B    15     Bb
        5     A

尝试了这个SELECT * FROM (SELECT*, 1 sortby FROM TableA ) a,(SELECT *, 2 sortby FROM TableB ) b ORDER BY a.sortby

提前致谢

1 个答案:

答案 0 :(得分:1)

像这样......(没试过)

SELECT
  a.col1,
  a.col2,
  b.column1,
  b.column2
FROM 
(
      SELECT
        col1,
        col2,
        ROW_NUMBER() OVER (ORDER BY col1 DESC) AS rownum
      FROM tableA
) AS a
FULL OUTER JOIN 
(
      SELECT
        column1,
        column2,
        ROW_NUMBER() OVER (ORDER BY column1 DESC) AS rownum
      FROM tableB
) AS b
ON a.rownum = b.rownum