Oracle 计算相同和不同的出现次数

Oracle counting number of SAME and DIFFERENT occurrences

我有一个包含两个 CLOBS 的 table,我正在对其进行重新设计以使其每行包含 1 个 clob。

我在下面有一个查询,用于识别 CLOBS 是否相同,它工作正常。

我可以在查询中使用一些帮助,它总结了相同和不同列的数量。

我希望生成如下输出:

Total_rows  same_cnt different_cnt
3     1   2

如有任何帮助,我们将不胜感激。感谢所有回答的人。



CREATE table table_x(
seq_num integer  GENERATED BY DEFAULT AS IDENTITY (START WITH 1) NOT NULL,
val NUMBER, 
clob1 CLOB,
clob2 CLOB);


insert into table_x (val, clob1, clob2) values (1,'aaaaaaaaaa','aaaaaaaaaa');

insert into table_x (val, clob1, clob2) values (1,'aaaaa','aaaaaaaaaa');

insert into table_x (val, clob1, clob2) values (2,'Xaaaaaaaaa','aaaaaaaaaa');


SELECT x.*,
       CASE DBMS_LOB.compare (x.clob1, x.clob2) WHEN 0 THEN 'SAME' ELSE 'DIFFERENT' END    AS comparison
  FROM table_x x;

SEQ_NUM    VAL    CLOB1    CLOB2    COMPARISON
1    1    aaaaaaaaaa    aaaaaaaaaa    SAME
2    1    aaaaa    aaaaaaaaaa    DIFFERENT
3    2    Xaaaaaaaaa    aaaaaaaaaa    DIFFERENT

您可以使用 条件 SUM 如下

with dt as (
SELECT x.*,
       CASE DBMS_LOB.compare (x.clob1, x.clob2) WHEN 0 THEN 'SAME' ELSE 'DIFFERENT' END    AS comparison
  FROM table_x x)
select 
count(*) total,
sum(case when comparison = 'DIFFERENT' then 1 else 0 end) as cnt_different,
sum(case when comparison = 'SAME' then 1 else 0 end) as cnt_same
from dt 

哪个returns

     TOTAL CNT_DIFFERENT   CNT_SAME
---------- ------------- ----------
         3             2          1