+---+-----+---+------+
| id| name|sal|gender|
+---+-----+---+------+
| 1| abc| -| M|
| 2| xyz| -| F|
| 3| pqr| -| M|
| 4|kiran| -| M|
| 5|kiran| -| F|
+---+-----+---+------+
编写sql查询/ spark sql以获取具有性别Male和female的名称
答案 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]|
+-----+------+
这将处理所有情况。希望对您有帮助