多次计算同一字段的配置单元

vof42yt1  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(352)

我需要计算有多少学生是从哪个学院,但当我使用下面的查询
选择学院,按学院从学生组中计数(*);
我得到了这个结果

结果显示同一所大学的不同计数我应该在这里做什么,这样我才能得到正确的大学计数

yuvru6vn

yuvru6vn1#

好像你在同一所大学有很多不同的名字,像这样

JIIT
"JIIT
jiit

尝试将它们规范化(转换为大写并删除 '"' ),所以会是一样的 JIIT 之后 group by :

select case when college = 'BSA' then 'BSA College of Technology'
        --add other cases
        else --rule for others
            trim(upper(regexp_replace(college,'"',''))) 
         end as college 
       ,COUNT(*)                                    as cnt 
   from students 
  group by 
        case when college = 'BSA' then 'BSA College of Technology'
        --add other cases
        else --rule for others
            trim(upper(regexp_replace(college,'"',''))) 
         end --the same sentence should be in group by, or use subquery instead
;

应用 case 转换更复杂的字符串,如 MJP ROHILKHAND 以及 M J P ROHILKHAND 都一样。
这是因为数据库没有规范化,输入也没有限制 College 按大学维度列出。

相关问题