如何创建一个与count distinct语句相关的百分比的附加列

krcsximq  于 2021-06-24  发布在  Hive
关注(0)|答案(4)|浏览(457)

我试图在一个表中查询每个不同的医学专业(例如肿瘤学家、儿科医生等),然后计算索赔的次数( claim_id )链接到它,我用这个:

select distinct specialization, count(distinct claim_id) AS Claim_Totals
from table1
group by specialization
order by Claim_Totals DESC

但是,我还想包括一个额外的列,它列出了表中每个专业所占的百分比(基于 claim_id 与之相关)。举个例子,如果总共有100个索赔,“心脏病Maven”有25个 claim_id 与之相关的记录,“肿瘤学家”有15个,“普通外科医生”有10个,以此类推,我希望输出如下:

specialization | Claims_Totals | PERCENTAGE
___________________________________________
cardiologist       25               25%
oncologist         15               15%
general surgeon    10               10%
p4rjhz4m

p4rjhz4m1#

你能做到吗?我不熟悉巴巴拉斯的语法。如果这样做的话,它会更简洁更好。

select specialization, count(distinct claim_id) AS Claim_Totals, count(distinct claim_id)/total_claims
from table1 
INNER JOIN ( SELECT COUNT(DISTINCT claim_id)*1.0000 total_claims AS total_claims 
               FROM table1 ) TMP
   ON 1 = 1
group by specialization
order by Claim_Totals DESC

select specialization, 
       count(distinct claim_id) AS claim_by_spec,
       count(distinct claim_id)/
       ( SELECT COUNT(DISTINCT claim_id)*1.0000
               FROM table1 ) AS percentage_calc
from table1 
group by specialization
order by Claim_Totals DESC
gijlo24d

gijlo24d2#

因为你正在使用 count(distinct) ,窗口函数不太有用。您可以尝试:

select t1.specialization,
       count(distinct t1.claim_id) AS Claim_Totals,
       count(distinct t1.claim_id) / tt1.num_claims
from table1 t1 cross join
     (select count(distinct claim_id) as num_claims
      from table1
     ) tt1
group by t1.specialization
order by Claim_Totals DESC
zbq4xfa0

zbq4xfa03#

你可以用 ,concat_ws('',count(distinct claim_id),'%') as percentage,concat(count(distinct claim_id),'%') as percentage 添加到选择列表的尾部
顺便说一句, distinct 在专门化之前,选择列表是多余的,因为已经包含在groupby列表中。

dfuffjeb

dfuffjeb4#

你可以用 sum(count(distinct)) over() 得到总体索赔,并在分母中使用它来得到百分比。

select specialization
      ,count(distinct claim_id) AS Claim_Totals
      ,round(100*count(distinct claim_id)/sum(count(distinct claim_id)) over(),3) as percentage
from table1
group by specialization

相关问题