SQL Oracle-单独ID的情况

时间:2018-08-09 08:13:59

标签: sql oracle plsql

我有一些(千个;)ID,并且我正在尝试分别检查它们的关系。但是我在独立检查每个ID的关系时遇到问题。实际上,我的代码会检查所有ID的内容。

例如表

ID  Service 
1   A
1   A1
2   A
2   B
3   A
3   A1

SQL代码

SELECT a.ID, a.service,
CASE 
    WHEN a.service IN ('A','A1') THEN 'Yes'
    ELSE 'No'
END      
FROM t1 a

输出

ID  Service  RELATION
1   A        YES
1   A1       YES
2   A        NO
2   B        NO
3   A        YES
3   A1       YES

5 个答案:

答案 0 :(得分:0)

首先,您应该根据服务的字母顺序对表格进行排序。 然后创建一个数组,并使用所有服务填充它:

DECLARE 
   CURSOR c_t1 is 
   SELECT  service FROM t1; 
   type c_list is varray (6) of t1.service%type; 
   service_list c_list := c_list(); 
   counter integer :=0; 
BEGIN 
   FOR n IN c_t1 LOOP 
      counter := counter + 1; 
      service_list.extend; 
      service_list(counter)  := n.service; 
      dbms_output.put_line('Service('||counter ||'):'||service_list(counter)); 
   END LOOP; 
END; 
/ 

然后遍历表值:

DECLARE
        v_count number DEFAULT 0;
BEGIN
    FOR i IN (SELECT t1.ID, t1.service FROM t1)
    LOOP
        FOR j IN (service_list.COUNT)
        LOOP
            CASE 
                WHEN t1.service IN (service_list[i], service_list[i+1]) THEN 'Yes'
                ELSE 'No'
            END      
            v_COUNT := v_COUNT + 1;
        END LOOP;
    END LOOP;
    DBMS_OUTPUT.PUT_LINE(v_COUNT);
END;
/

答案 1 :(得分:0)

以下查询有效,但我不确定其性能如何。

WITH a1
     AS (SELECT id, 1 AS here
         FROM   services
         WHERE  service = 'A1')
   , a
     AS (SELECT id, 1 AS here
         FROM   services
         WHERE  service = 'A')
SELECT driver.id
     , driver.service
     , CASE
          WHEN NVL(a1.here, 0) + NVL(a.here, 0) > 1 THEN 'YES'
          ELSE 'NO'
       END
FROM   services driver
       LEFT OUTER JOIN a1 ON a1.id = driver.id
       LEFT OUTER JOIN a ON a.id = driver.id

答案 2 :(得分:0)

  

但是我在检查每个ID的关系时遇到问题   独立地

这就是为什么建议您创建一个新表来存储服务之间所有唯一关系的原因。这样可以更好地比较关系并提供所需的输出。

您应该首先创建关系表,该表存储这样的值。

|rid| S1 | S2 |
--- |----|----|
|1  |  A | A1 |

现在,要生成所需的输出,您应该首先使用自连接来获取与常见ID的一对关系。

SELECT a.id, 
       a.service,
       b.service
FROM   t1 a 
       join t1 b 
         ON ( a.service != b.service 
              AND a.id = b.id ) ;

一旦有了这样的组合,您所要做的就是使用带有关系表的OUTER JOIN检查该关系的组合是否存在,这将帮助您获得“是”或“否”的输出根据存在。

SELECT a.id, 
       b.service, 
       CASE 
         WHEN r.id IS NULL THEN 'NO' 
         ELSE 'YES' 
       END AS relation 
FROM   t1 a 
       join t1 b 
         ON ( a.service != b.service 
              AND a.id = b.id ) 
       left join rel r 
              ON ( ( r.s1 = a.service 
                     AND r.s2 = b.service ) 
                    OR ( r.s2 = a.service 
                         AND r.s1 = b.service ) ); 

Demo

答案 3 :(得分:0)

我会使用 window 函数:

select t1.*, (case when sum(case when service not in ('A', 'A1') then 1 else 0 end) over (partition by id) = 0
                   then 'yes' else 'no'
              end) as Relation
from t1; 

答案 4 :(得分:0)

这将起作用,并且您必须为其使用解码功能:

create table ex2(
no1 number,
no2 varchar(20));
insert into ex2 values(1,    'A');
insert into ex2 values(1   , 'A1');
insert into ex2 values(2   , 'A');
insert into ex2 values(2   , 'B');
insert into ex2 values(3   , 'A');
insert into ex2 values(3   , 'A1');
select * from ex2;

SELECT no1, no2 ,
decode(no2,'A','yes','A1','yes','no')      
FROM ex2;

输出:

1   A   yes
2   A   yes
2   B   no
3   A   yes
3   A1  yes
1   A1  yes

您的表查询为:

SELECT ID, service ,
decode(service,'A','yes','A1','yes','no')
FROM ex2;