Equivalent of R cbind() function in Oracle?

纵然是瞬间 提交于 2019-12-23 22:23:35

问题


I am new to sql and struggling to solve this very simple task.

Considering,

    with table1 as (select '1' col1 from dual union
select '2' col1 from dual union
select 'NO_PATTERN' col1 from dual union
select 'RANDOM_STUFF' col1 from dual)
    select * from table1;

and,

    with table2 as (select 'aaa' col2 from dual union
select '4' col2 from dual union
select 'qwewqeq' col2  from dual
union select 'UUUUUU' col2 from dual)
    select * from table2;

I want to perform a cbind() between the two columns into a new table which is the "vertical union" of table1.[col1] and table2[col2].

The EXPECTED solution is:

    with solution as (select '1' col1, 'aaa' col2 from dual union
select '2' col1, '4' col2 from dual union
select 'NO_PATTERN'  col1, 'qwewqeq' col2 from dual union
select 'RANDOM_STUFF'  col1, 'UUUUUU'  col2 from dual)
    select * from solution;

Any idea?


回答1:


In Oracle you need to explicitly define some value to get ordered data; without that, you could have different results every time you run a query.

With your data, this:

WITH table1 AS
         (SELECT 1 rn, '1'            col1 FROM DUAL UNION
          SELECT 2 rn, '2'            col1 FROM DUAL UNION
          SELECT 3 rn, 'NO_PATTERN'   col1 FROM DUAL UNION
          SELECT 4 rn, 'RANDOM_STUFF' col1 FROM DUAL),
     table2 AS
         (SELECT 1 rn, 'aaa'     col2 FROM DUAL UNION
          SELECT 2 rn, '4'       col2 FROM DUAL UNION
          SELECT 3 rn, 'qwewqeq' col2 FROM DUAL UNION
          SELECT 4 rn, 'UUUUUU'  col2 FROM DUAL)
SELECT col1, col2, t1.rn
from (select  row_number() over (order by rn) as rn, col1 from table1 ) t1
       inner join 
     (select row_number() over (order by rn) as rn, col2 from table2 ) t2
     on (t1.rn = t2.rn)

gives:

COL1         COL2            RN
------------ ------- ----------
1            aaa              1
2            4                2
NO_PATTERN   qwewqeq          3
RANDOM_STUFF UUUUUU           4

Without an explicit ordering, for exampe, this

WITH table1 AS
         (SELECT '1'            col1 FROM DUAL UNION
          SELECT '2'            col1 FROM DUAL UNION
          SELECT 'NO_PATTERN'   col1 FROM DUAL UNION
          SELECT 'RANDOM_STUFF' col1 FROM DUAL),
     table2 AS
         (SELECT 'aaa'     col2 FROM DUAL UNION
          SELECT '4'       col2 FROM DUAL UNION
          SELECT 'qwewqeq' col2 FROM DUAL UNION
          SELECT 'UUUUUU'  col2 FROM DUAL)
SELECT col1, col2, t1.rn
from (select  rownum as rn, col1 from table1 ) t1
       inner join 
     (select rownum as rn, col2 from table2 ) t2
     on (t1.rn = t2.rn)

gives

COL1         COL2            RN
------------ ------- ----------
1            4                1
2            UUUUUU           2
NO_PATTERN   aaa              3
RANDOM_STUFF qwewqeq          4



回答2:


If you don't want to order them, this would be a simple way to do it -

select *, ROW_NUMBER() OVER (ORDER BY (SELECT 100)) AS SNO into #0 FROM Table 2

select *, ROW_NUMBER() OVER (ORDER BY (SELECT 100)) AS SNO into #1 FROM Table1

select a.*, b.* into #3 from #0 a join #1 b on a.SNO = b.SNO

Got the idea from here -

https://blog.sqlauthority.com/2015/05/05/sql-server-generating-row-number-without-ordering-any-columns/



来源:https://stackoverflow.com/questions/50720672/equivalent-of-r-cbind-function-in-oracle

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!