2013-11-21 106 views
0

我有2個SELECT陳述,無論是從dirrefernt表將數據插入到臨時表

返回13行

我想創建1和臨時表2列插入2個結果行到2列。有沒有辦法做到這一點?

所以

1 - SELECT INPOS FROM TABLE1回報

1,2,3,4,5,6,7,18,9,10,11,12,13

2 - SELECT CODE FROM TABLE2回報

CODEa所,CODEB,編解碼器,編碼CODEE,CODEF,CODEG,CODEH,CODEI,CODEJ,CODEK,CODEL,CODEM

我想我的臨時表是

1 | CODEA


2 | CODEB


3 | CODEC


4 | CODED


5 | CODEE


6 | CODEF


7 | CODEG


8 | CODEH


9 | CODEI


10 | CODEJ


11 | CODEK


12 | CODEL


13 | CODEM

+0

你不必在你的結構連接表的方法嗎?通過ID或什麼? –

+0

@FilipeSilva - 遺憾的是沒有。如果這是Oracle,我可以使用rownum。從字面上看,我知道兩個SELECT語句都會返回13個值(按正確的順序),我想用這些值並排填充一個新的2列表 – Mike

回答

0

嘗試是這樣的:

SELECT a.impos, b.code 
FROM (
    (
    SELECT impos, RANK() OVER (ORDER BY impos ASC) AS link 
    FROM table1 
    ) AS a INNER JOIN (
    SELECT code, RANK() OVER (ORDER BY code ASC) AS link 
    FROM table2 
    ) AS b ON a.link = b.link 
) 

sqlfiddle demo

+0

非常棒!謝謝@Filipe – Mike

2

試試這個:

WITH T1 AS (
    SELECT ROW_NUMBER() OVER(ORDER BY INPOS) ID, INPOS FROM TABLE1 
), 
WITH T2 AS 
(
    SELECT ROW_NUMBER() OVER(ORDER BY CODE) ID, CODE FROM TABLE2 
), 
SELECT T1.INPOS, T2.CODE 
FROM T1 INNER JOIN T2 ON T1.ID = T2.ID