得到具有性别和性别的名字

时间:2019-12-02 11:24:10

标签: sql apache-spark-sql

+---+-----+---+------+
| id| name|sal|gender|
+---+-----+---+------+
|  1|  abc|  -|     M|
|  2|  xyz|  -|     F|
|  3|  pqr|  -|     M|
|  4|kiran|  -|     M|
|  5|kiran|  -|     F|
+---+-----+---+------+

编写sql查询/ spark sql以获取具有性别Male和female的名称

2 个答案:

答案 0 :(得分:2)

一个简单的方法是:

select name
from t
group by name
having min(gender) <> max(gender);

如果您的性别超过两个,请在汇总之前进行过滤:

select name
from t
where gender in ('M', 'F')
group by name
having min(gender) <> max(gender);

答案 1 :(得分:1)

您可以在数据框中以更简洁的方式实现这一目标。

scala> var dfd = Seq((1,  "abc",1,"M"),(2,  "xyz",2,"F"),(3,  "pqr",3,"M"),(6,  "abc",7,"M"),(4,"kiran",4,"M"),(5,"kiran",5,"F")).toDF("id","name","sal","gender")

scala> dfd.groupBy("name").agg(collect_set("gender").as("_both")).filter(size(col("_both"))>1).show
+-----+------+
| name| _both|
+-----+------+
|kiran|[M, F]|
+-----+------+

这将处理所有情况。希望对您有帮助