- Mastering Apache Spark 2.x(Second Edition)
- Romeo Kienzler
- 198字
- 2021-07-02 18:55:30
Using SQL subqueries
It is also possible to use subqueries in ApacheSparkSQL. In the following example, a SQL query uses an anonymous inner query in order to run aggregations on Windows. The encapsulating query is making use of the virtual/temporal result of the inner query, basically removing empty columns:
val result = spark.sql("""
SELECT * from (
SELECT
min(temperature) over w as min_temperature,
max(temperature) over w as max_temperature,
min(voltage) over w as min_voltage,
max(voltage) over w as max_voltage,
min(flowrate) over w as min_flowrate,
max(flowrate) over w as max_flowrate,
min(frequency) over w as min_frequency,
max(frequency) over w as max_frequency,
min(hardness) over w as min_hardness,
max(hardness) over w as max_hardness,
min(speed) over w as min_speed,
max(speed) over w as max_speed
FROM washing_flat
WINDOW w AS (ORDER BY ts ROWS BETWEEN CURRENT ROW AND 10 FOLLOWING)
)
WHERE min_temperature is not null
AND max_temperature is not null
AND min_voltage is not null
AND max_voltage is not null
AND min_flowrate is not null
AND max_flowrate is not null
AND min_frequency is not null
AND max_frequency is not null
AND min_hardness is not null
AND min_speed is not null
AND max_speed is not null
""")
The result of the subqueries is as follows:

推薦閱讀
- 手機安全和可信應用開發指南:TrustZone與OP-TEE技術詳解
- Web前端開發技術:HTML、CSS、JavaScript(第3版)
- Game Programming Using Qt Beginner's Guide
- 趣學Python算法100例
- Mastering matplotlib
- ASP.NET動態網頁設計教程(第三版)
- Scratch真好玩:教小孩學編程
- The Data Visualization Workshop
- Spring Boot Cookbook
- Learning OpenCV 3 Computer Vision with Python(Second Edition)
- Mastering Xamarin.Forms(Second Edition)
- Android移動開發案例教程:基于Android Studio開發環境
- 寫給程序員的Python教程
- Arduino電子設計實戰指南:零基礎篇
- Spring Data JPA從入門到精通