使用 group by -SQL (Impala) 计算两个表之间的百分比
Calculate percentage between two tables with group by -SQL (Impala)
我正在 Impala (Cloudera) 上工作,我有两个 tables,Customers 和 Arrangements。客户 table 有以下列:
customercrs | customertype|
------------+-------------+
1000 | NP |
100000 | NP |
100001 | NP |
100002 | GROUP |
100023 | GROUP |
100024 | INDIRECT |
排列table:
customercrs | arrangementid|
------------+--------------+
1000 | 11000000361 |
100000 | 11000000370 |
100000 | 11000000434 |
100000 | 11000000426 |
100001 | 11000000418 |
100001 | 11000000400 |
100001 | 11000000396 |
100001 | 11000000388 |
100002 | 11000000591 |
100002 | 11000000582 |
100023 | 11000000574 |
100024 | 11000000566 |
100024 | 11000000558 |
我想计算每种客户类型的安排百分比。类似于:
customertype | percentage |
-------------+-------------+
NP | 62% |
GROUP | 23% |
INDIRECT | 15% |
我尝试了以下 sql 查询,但没有成功。有什么想法吗?
select customertype, count(*)/(select count(*) from arrangements)
from customers as a, arrangements_sample as b
where a.customercrs = b.customercrs
group by a.customertype
谢谢!!!
我会将 window 函数与显式 JOIN
一起使用,但是,您的解决方案似乎没问题(对于 Impala 以外的其他 DBMS)
select customertype,
(count(*) * 100) / sum(count(*)) over () percentage
from customers as a
join arrangements_sample as b on a.customercrs = b.customercrs
group by a.customertype
尝试加入subselect,我用max作为分组函数,但是min或avg也可以...
select customertype, count(*)/max(c.total)
from customers as a, arrangements_sample as b, (select count(*) as total from
arrangements) as c
where a.customercrs = b.customercrs
group by a.customertype
您需要每个客户类型参与安排的总数。所以请尝试以下查询。
select main.customertype,cast((cast(main.participation as decimal(10,2))/main.total)*100 as decimal(10,2) ) 作为参与
from (select customertype,COUNT(1) as participation,(select COUNT(1) from arrangements) as total from 安排 a
b.customercrs = a.customercrs 上的内部加入客户 b
按 b.customertype) 分组为主要
我正在 Impala (Cloudera) 上工作,我有两个 tables,Customers 和 Arrangements。客户 table 有以下列:
customercrs | customertype|
------------+-------------+
1000 | NP |
100000 | NP |
100001 | NP |
100002 | GROUP |
100023 | GROUP |
100024 | INDIRECT |
排列table:
customercrs | arrangementid|
------------+--------------+
1000 | 11000000361 |
100000 | 11000000370 |
100000 | 11000000434 |
100000 | 11000000426 |
100001 | 11000000418 |
100001 | 11000000400 |
100001 | 11000000396 |
100001 | 11000000388 |
100002 | 11000000591 |
100002 | 11000000582 |
100023 | 11000000574 |
100024 | 11000000566 |
100024 | 11000000558 |
我想计算每种客户类型的安排百分比。类似于:
customertype | percentage |
-------------+-------------+
NP | 62% |
GROUP | 23% |
INDIRECT | 15% |
我尝试了以下 sql 查询,但没有成功。有什么想法吗?
select customertype, count(*)/(select count(*) from arrangements)
from customers as a, arrangements_sample as b
where a.customercrs = b.customercrs
group by a.customertype
谢谢!!!
我会将 window 函数与显式 JOIN
一起使用,但是,您的解决方案似乎没问题(对于 Impala 以外的其他 DBMS)
select customertype,
(count(*) * 100) / sum(count(*)) over () percentage
from customers as a
join arrangements_sample as b on a.customercrs = b.customercrs
group by a.customertype
尝试加入subselect,我用max作为分组函数,但是min或avg也可以...
select customertype, count(*)/max(c.total)
from customers as a, arrangements_sample as b, (select count(*) as total from
arrangements) as c
where a.customercrs = b.customercrs
group by a.customertype
您需要每个客户类型参与安排的总数。所以请尝试以下查询。
select main.customertype,cast((cast(main.participation as decimal(10,2))/main.total)*100 as decimal(10,2) ) 作为参与 from (select customertype,COUNT(1) as participation,(select COUNT(1) from arrangements) as total from 安排 a b.customercrs = a.customercrs 上的内部加入客户 b 按 b.customertype) 分组为主要