从表中生成count之后,需要在配置单元中生成percentage列

kb5ga3dv  于 2021-06-24  发布在  Hive
关注(0)|答案(1)|浏览(564)

我创造了以下 HIVE 有助于从源配置单元表确定个体的计数和类型的表。现在,我需要从以下内容中创建进一步的百分比突破:

CREATE TABLE hh_member_type AS 
SELECT count(*) AS count ,
CASE WHEN (LEAD_HOUSEHLD_STATUS = 'D') THEN 'Deceased' 
     WHEN (LEAD_HOUSEHLD_STATUS = 'H') THEN 'Head' 
     WHEN (LEAD_HOUSEHLD_STATUS = 'P') THEN 'Aged Parent At Home' 
     WHEN (LEAD_HOUSEHLD_STATUS = 'U') THEN 'Unknown' 
     WHEN (LEAD_HOUSEHLD_STATUS = 'W') THEN 'Spouse' 
     WHEN (LEAD_HOUSEHLD_STATUS = 'Y') THEN 'Young Adult' ELSE 'NULL' 
 END AS HH_STATUS 
FROM ( 
SELECT  LEAD_HOUSEHLD_STATUS FROM customer 
UNION ALL SELECT MEM1_HH_STATUS FROM customer 
UNION ALL SELECT MEM2_HH_STATUS FROM customer 
UNION ALL SELECT MEM3_HH_STATUS FROM customer 
UNION ALL SELECT MEM4_HH_STATUS FROM customer 
UNION ALL SELECT MEM5_HH_STATUS FROM customer 
UNION ALL SELECT MEM6_HH_STATUS FROM customer 
UNION ALL SELECT MEM7_HH_STATUS FROM customer 
UNION ALL SELECT MEM8_HH_STATUS FROM customer)A 
WHERE LEAD_HOUSEHLD_STATUS IN ('H','D','P','U','W','Y') 
GROUP BY LEAD_HOUSEHLD_STATUS ;

上述代码的输出如下:

hive> select * from hh_member_type ;
OK
277 Deceased
20636   Head
3547    Aged Parent At Home
18926   Unknown
16184   Spouse
7082    Young Adult
Time taken: 0.195 seconds, Fetched: 6 row(s)

我尝试使用下面的代码创建百分比中断,但没有得到所需的输出。有什么建议都可以。

CREATE TABLE hh_member_percent AS 
SELECT a.hh_status,round(a.count/sum(a.count) * 100 ,2) 
  from hh_member_type a 
 group by a.count,a.hh_status ;
lb3vh1jj

lb3vh1jj1#

使用分析总和():

SELECT a.hh_status,round(a.count/sum(a.count) over() * 100 ,2) 
  from hh_member_type a 
;

相关问题