Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql Oracle连接到多个列中的任一列_Sql_Oracle - Fatal编程技术网

Sql Oracle连接到多个列中的任一列

Sql Oracle连接到多个列中的任一列,sql,oracle,Sql,Oracle,我有一张关系表 NUM1 | NUM2 | NUM3 -- --- ----- 1 2 3 2 4 5 3 4 null 3 4 null 以及实际信息表,其中NUM是主键 NUM | A_LOT_OF_OTHER_INFO --- -------------------- 1 asdff 2 werwr 3 erert 4 ghfgh

我有一张关系表

NUM1 | NUM2 | NUM3
 --   ---   -----
 1     2     3    
 2     4     5
 3     4     null
 3     4     null
以及实际信息表,其中NUM是主键

 NUM | A_LOT_OF_OTHER_INFO
 ---  --------------------
   1     asdff    
   2     werwr
   3     erert
   4     ghfgh
   5     cvbcb
我想创建一个视图来查看在关系表的NUM1、NUM2、NUM3中出现的NUM的计数

我的观点

 NUM | A_LOT_OF_OTHER_INFO | TOTAL_COUNT
 ---  --------------------  ------------
   1     asdff                  1
   2     werwr                  2
   3     erert                  3
   4     ghfgh                  3
   5     cvbcb                  1

我可以通过从关系表中进行三次选择并合并它们来实现这一点,但我不想使用UNION,因为这些表有很多记录,我的_视图已经足够大了,我正在寻找一种更好的方法来连接视图中的关系表。你能建议一种方法吗?

我建议一个相关的子查询:

select i.*,
       (select ((case when r.num1 = i.num then 1 else 0 end) +
                (case when r.num2 = i.num then 1 else 0 end) +
                (case when r.num3 = i.num then 1 else 0 end)
               )
        from relation r
        where i.num in (r.num1, r.num2, r.num3)
       ) as total_count
from info i;
如果性能是一个考虑因素,那么使用
left join
s可能会更快:

select i.*,
       ((case when r1.num1 is not null then 1 else 0 end) +
        (case when r2.num1 is not null then 1 else 0 end) +
        (case when r3.num1 is not null then 1 else 0 end)
       ) as total_count
from info i left join
     relation r1
     on i.num = r1.num1 left join
     relation r2
     on i.num = r2.num2 left join
     relation r3
     on i.num = r3.num3;

特别是,这将优化使用
关系
上的三个单独索引:
关系(num1)
关系(num2)
,和
关系(num3)
,我建议使用一个相关子查询:

select i.*,
       (select ((case when r.num1 = i.num then 1 else 0 end) +
                (case when r.num2 = i.num then 1 else 0 end) +
                (case when r.num3 = i.num then 1 else 0 end)
               )
        from relation r
        where i.num in (r.num1, r.num2, r.num3)
       ) as total_count
from info i;
如果性能是一个考虑因素,那么使用
left join
s可能会更快:

select i.*,
       ((case when r1.num1 is not null then 1 else 0 end) +
        (case when r2.num1 is not null then 1 else 0 end) +
        (case when r3.num1 is not null then 1 else 0 end)
       ) as total_count
from info i left join
     relation r1
     on i.num = r1.num1 left join
     relation r2
     on i.num = r2.num2 left join
     relation r3
     on i.num = r3.num3;

特别是,这将优化使用
关系
上的三个独立索引:
关系(num1)
关系(num2)
,和
关系(num3)
,,似乎您想要的是UNPIVOT。在这种情况下,使用交叉联接可能最容易做到:

select   NUM, count(*) as TOTAL_COUNT
from     ( 
           select decode(column_value, 1, NUM1, 2, NUM2, 3, NUM3) as NUM
           from   RELATION cross join table(sys.odcinumberlist(1,2,3))
         )
group by NUM
;

然后将其连接到第二个表;连接部分在这里实际上是不相关的。

似乎您想要的是取消连接。在这种情况下,使用交叉联接可能最容易做到:

select   NUM, count(*) as TOTAL_COUNT
from     ( 
           select decode(column_value, 1, NUM1, 2, NUM2, 3, NUM3) as NUM
           from   RELATION cross join table(sys.odcinumberlist(1,2,3))
         )
group by NUM
;

然后将其连接到第二个表;连接部分在这里真的不相关。

我要尝试的是取消关联表的绑定

之后,加入值的info表,并计算val重复的次数

create table relation(num1 int,num2 int, num3 int);

insert into relation values(1,2,3);
insert into relation values(2,4,5);
insert into relation values(3,4,null);

create table info(num int, a_lot_of_other_info varchar2(100));
insert into info
   select 1,'asdff' from dual union all
   select 2,'werwr' from dual union all
   select 3,'erert' from dual union all
   select 4,'ghfgh' from dual union all
   select 5,'cvbcb' from dual 

 select a.num
        ,max(a_lot_of_other_info) as a_lot_of_other_info
        ,count(*) as num_of_times
   from info a
   join (select val
           from relation a
         unpivot(val for x in (num1,num2,num3))
         )b
      on a.num=b.val  
 group by a.num 
 order by 1

我要尝试的是取消关联表

之后,加入值的info表,并计算val重复的次数

create table relation(num1 int,num2 int, num3 int);

insert into relation values(1,2,3);
insert into relation values(2,4,5);
insert into relation values(3,4,null);

create table info(num int, a_lot_of_other_info varchar2(100));
insert into info
   select 1,'asdff' from dual union all
   select 2,'werwr' from dual union all
   select 3,'erert' from dual union all
   select 4,'ghfgh' from dual union all
   select 5,'cvbcb' from dual 

 select a.num
        ,max(a_lot_of_other_info) as a_lot_of_other_info
        ,count(*) as num_of_times
   from info a
   join (select val
           from relation a
         unpivot(val for x in (num1,num2,num3))
         )b
      on a.num=b.val  
 group by a.num 
 order by 1


这将显示数字显示的行数,但不会计算外观本身。如果一行的所有三个列值都等于1,则OP希望计数为3,而不是1。对于INFO表中的每一行,不是第一次运行吗?@mathguy我们确信NUM1 NUM2 NUM3不同或为空,但感谢您的通知。@mathguy。(1) 这只适用于第一个查询。(2) 现在已修复。第二个查询读取基表三次,这正是OP希望避免的。这将显示数字出现的行数,但不会计算出现的行数。如果一行的所有三个列值都等于1,则OP希望计数为3,而不是1。对于INFO表中的每一行,不是第一次运行吗?@mathguy我们确信NUM1 NUM2 NUM3不同或为空,但感谢您的通知。@mathguy。(1) 这只适用于第一个查询。(2) 现在已修复。第二个查询读取基表三次,这正是OP希望避免的。请注意,OP也希望保留
INFO
中的另一列;以这种方式安排查询将不允许他这样做。(另外,只有当
NUM
INFO
中没有重复项时,查询才会为每个
NUM
生成正确的计数;也许是这样,它肯定在提供的示例中,但无论如何都要记住这一点。)同意,我现在加入了“其他信息”的a_lot_列,是的,我假设NUM列是唯一的,我使用了这里的方法,不需要使用MAX(大量的其他信息),因为它确定NUM是唯一的,没有重复的行;以这种方式安排查询将不允许他这样做。(另外,只有当
NUM
INFO
中没有重复项时,查询才会为每个
NUM
生成正确的计数;也许是这样,它肯定在提供的示例中,但无论如何都要记住这一点。)同意,我现在加入了“其他信息”的a_lot_列,是的,我假设NUM列是唯一的,我在这里使用了这种方法,不需要使用MAX(一大堆其他信息),因为它确定NUM是唯一的,没有重复的行。[代码:904,SQL状态:42000]ORA-00904:“NUM”:无效标识符@mCeviker-对不起,我把GROUP BY子句放错了位置;当然,它应该在外部查询中,而不是在子查询中。正确。抱歉,我发现了,但忘了在这里更新。[代码:904,SQL状态:42000]ORA-00904:“NUM”:无效标识符@mCeviker-抱歉,我将GROUP BY子句放在错误的位置;当然,它应该在外部查询中,而不是在子查询中。正确。对不起,我想出来了,但忘了在这里更新。