我为oracle编写了Majority sql查询,如下所述:
Majority function in SQL
但我的要求略有不同。如果没有找到列值为多数,则应在查询中返回具有空值的单行。我尝试过像:
select CASE WHEN count(*) > 0 THEN tbl2.DATA ELSE NULL END AS "Majority_column1_" from (
select distinct DATA
from (
select DATA, NAME,
count(*) over () as total_count,
count(*) over (partition by DATA) as DATA_count
from MAJORITY_TEST
) tbl1 where tbl1.DATA_count > tbl1.total_count / 2) tbl2;
想法是检查总数是否。返回的行数是> 0然后返回行值,因为它返回null。但从语法上讲它是不正确的。
有人可以建议我对此进行正确的查询。
谢谢
答案 0 :(得分:0)
根据您对多数意义的定义,50%的出现次数是该值,您只能返回零或一个值。因此,您可以使用max()
:
select max(DATA)
from (
select DATA, NAME,
count(*) over () as total_count,
count(*) over (partition by DATA) as DATA_count
from MAJORITY_TEST
) tbl1 where tbl1.DATA_count > tbl1.total_count / 2;
对于存在多数值的数据:
insert into majority_test (data, name)
select 10, 'A' from dual
union all select 20, 'B' from dual
union all select 20, 'C' from dual
union all select 20, 'D' from dual;
这给出了:
MAX(DATA)
----------
20
汇总功能意味着您不需要distinct
。没有多数,通过update majority_test set data = 10 where name = 'D'
,相同的查询给出:
MAX(DATA)
----------
即。单行,空值。
答案 1 :(得分:0)
这是获取值的方法。对data_count
和total_count
进行计算。然后选择值data_count
最大的行。如果超过阈值,则输出DATA
值。否则,输出NULL
:
select (case when data_count > total_count / 2 then DATA end)
from (select DATA,
count(*) as DATA_count, over () as data_count,
sum(count(*)) over () as total_count
from MAJORITY_TEST
group by DATA
order by data_count desc
) t
where rownum = 1;
请注意,子查询使用带分析函数的显式聚合。这只是一种替代方法。