我试图获得一组特定值所具有的行的百分比。最好用例子解释。我可以非常简单地使用ratio-to-report函数和over()来完成每列的操作,但是我遇到了多个分组的问题
假设表有2列:
column a column b
1000 some data
1100 some data
2000 some data
1400 some data
1500 some data
通过以下查询,我可以获得此域集,每个域集占总行数的20%
select columna, count(*), trunc(ratio_to_report(count(columna)) over() * 100, 2) as perc
from table
group by columna
order by perc desc;
然而,我需要的是例如确定百分比&包含1000,1400或2000的行数;从查看它,你可以告诉它60%,但需要一个查询来返回。这需要高效,因为查询将针对数百万行运行。就像我之前说过的那样,我有一个单一的价值及其百分比,但多数是投掷我的东西。
似乎我需要能够在某处放置一个IN子句,但每次都不会是这些值。如果有意义的话,我需要从另一个表中获取它的“IN”部分的值。我想我需要某种多重分组。
答案 0 :(得分:1)
潜在地,你正在寻找像
这样的东西SQL> ed
Wrote file afiedt.buf
1 with x as (
2 select 1000 a from dual
3 union all
4 select 1100 from dual
5 union all
6 select 1400 from dual
7 union all
8 select 1500 from dual
9 union all
10 select 2000 from dual
11 )
12 select (case when a in (1000,1400,1500)
13 then 1
14 else 0
15 end) bucket,
16 count(*),
17 ratio_to_report(count(*)) over ()
18 from x
19 group by (case when a in (1000,1400,1500)
20 then 1
21 else 0
22* end)
SQL> /
BUCKET COUNT(*) RATIO_TO_REPORT(COUNT(*))OVER()
---------- ---------- -------------------------------
1 3 .6
0 2 .4
答案 1 :(得分:0)
我不确定我完全理解这个要求,但你需要ratio_to_report
吗?看看以下内容,让我知道这与你想要的有多接近,我们可以在那里工作!
T1是包含样本数据的表
create table t1(a primary key) as
select 1000 as a from dual union all
select 1100 as a from dual union all
select 1400 as a from dual union all
select 1500 as a from dual union all
select 2000 as a from dual;
T2是您提到的查找表(获取ID列表)
create table t2(a primary key) as
select 1000 as a from dual union all
select 1400 as a from dual union all
select 2000 as a from dual;
T1-> T2的左连接将返回T1中与T2中所有匹配行配对的所有行。对于T1中不存在的每个A(T2),结果将填充为NULL。我们可以利用COUNT()
不计算(呵呵)空值的事实。
select count(t1.a) as num_rows
,count(t2.a) as in_set
,count(t2.a) / count(t1.a) as shr_in_set
from t1
left
join t2 on(t1.a = t2.a);
运行查询的结果是:
NUM_ROWS IN_SET SHR_IN_SET
---------- ---------- ----------
5 3 ,6