// 建立檢視 data.createOrReplaceTempView("Affairs") val df1 = spark.sql("SELECT * FROM Affairs WHERE age BETWEEN 20 AND 25") df1: org.apache.spark.sql.DataFrame = [affairs: double, gender: string ... 7 more fields] // 子查詢 val df2 = spark.sql("select gender, age,rating from ( SELECT * FROM Affairs WHERE age BETWEEN 20 AND 25 ) t ") df2: org.apache.spark.sql.DataFrame = [gender: string, age: double ... 1 more field] df2.show +------+----+------+ |gender| age|rating| +------+----+------+ | male|22.0| 3.0| |female|22.0| 3.0| | male|22.0| 5.0| +------+----+------+