HIVE获得选择任何课程的男女人数

时间:2018-07-11 06:58:58

标签: sql database hive

我有两个桌子,分别是学生和培训。学生和培训表如下。

Student
ID      name    age     sex     salary
1213    lavanya 18      Female  8000
1208    reshma  19      Female  14000
1207    bhavya  20      Female  15000
1212    Arshad  28      Male    20000
1209    kranthi 22      Male    22000
1210    Satish  24      Male    25000
1211    Krishna 25      Male    26000
1203    khaleel 34      Male    30000
1204    prasant 30      Male    31000
1206    laxmi   25      Female  35000
1205    kiran   20      Male    40000
1201    gopal   45      Male    50000
1202    manisha 40      Female  51000

Training
1       1201    csharp
2       1205    c
3       1201    c
4       1202    java
5       1205    java
6       1203    shell
7       1204    hadoop
8       1201    hadoop

现在,我想统计参加任何课程的男性和女性人数。 我在下面的查询中尝试过-

hive> select s.sex, count(*) from student join training t on s.id=t.sid group by s.sex;

但是此查询给出的输出为Female 2 Male 4 虽然预期结果应该是女性1男性2 请注意,这是使用的示例数据的简短形式。

6 个答案:

答案 0 :(得分:0)

这看起来像您的查询,但是-返回您提到的结果(1位女性,2位男性)。如果可能的话,发布您自己的SQL * Plus复制/粘贴会话(以我的示例为例),以便我们看到您的实际操作。

SQL> with student (id, name, sex) as
  2    (select 1, 'alex', 'm' from dual union
  3     select 2, 'rita', 'f' from dual union
  4     select 3, 'max',  'm' from dual union
  5     select 4, 'steve', 'm' from dual
  6    ),
  7  training (id, sid, course) as
  8    (select 1, 2, 'java' from dual union
  9     select 2, 3, 'c' from dual union
 10     select 3, 1, 'java' from dual
 11    )
 12  select s.sex, count(*)
 13  from student s join training t on t.sid = s.id
 14  group by s.sex;

S   COUNT(*)
- ----------
m          2
f          1

答案 1 :(得分:0)

我在MySQL和Oracle中尝试过,此查询还可以。

SELECT S.sex, count(*) 
FROM student s 
INNER JOIN training T on S.id = T.sid 
GROUP BY S.sex;

结果,女= 1,男= 2

答案 2 :(得分:0)

如果您只想按性别简单计数,为什么不使用

    select sex, count(*)
    from student
    group by sex
    order by sex

答案 3 :(得分:0)

使用exists

select s.sex, count(*)
from students s
where exists (select 1 from training t where t.sid = s.id);

join的问题在于,它会根据每个学生参加的培训次数对其进行计数。

答案 4 :(得分:0)

在这里我写了一个代码来接收您的数据:-

SELECT 
    final.ct_sex as sex,count(*) as num
FROM 
    (SELECT tb.sex as ct_sex FROM newschema.mytable AS tb JOIN (SELECT tr.ID,GROUP_CONCAT(tr.skill) as skills FROM newschema.train AS tr GROUP BY tr.ID) AS tp ON tb.ID = tp.ID) as final
group by
    final.ct_sex

答案 5 :(得分:0)

不确定为什么连接失败,但是下面的子查询给出了正确的输出。  选择性别,从薪水中计算(*),其中(从培训中选择sid)在薪水中的薪金(工资)依薪水.sex分组;