简体   繁体   English

是否等效于Oracle中的cbind()函数?

[英]Equivalent of R cbind() function in Oracle?

I am new to sql and struggling to solve this very simple task. 我是sql的新手,正在努力解决这一非常简单的任务。

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]. 我想在两列之间执行一个cbind()到一个新表中,该表是table1。[col1]和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? 任何想法?

In Oracle you need to explicitly define some value to get ordered data; 在Oracle中,您需要显式定义一些值以获取有序数据。 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

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://blog.sqlauthority.com/2015/05/05/sql-server-generating-row-number-without-ordering-any-columns/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM