Column EXISTS 하위 쿼리에 대한 개체를 반환합니다.
문법
exists()
Returns
Column
Column: EXISTS 하위 쿼리를 나타내는 개체입니다.
Notes
이 메서드는 exists 하위 쿼리에 관련 레코드가 있는지 확인하는 부울 열을 만드는 방법을 제공합니다. 이 메서드를 DataFrame사용하면 일치하는 레코드가 관련 데이터 세트에 있는지 여부에 따라 행을 필터링할 수 있습니다. 결과 Column 개체는 필터링 조건에서 직접 또는 계산 열로 사용할 수 있습니다.
예제
data_customers = [
(101, "Alice", "USA"), (102, "Bob", "Canada"), (103, "Charlie", "USA"),
(104, "David", "Australia")
]
data_orders = [
(1, 101, "2023-01-15", 250), (2, 102, "2023-01-20", 300),
(3, 103, "2023-01-25", 400), (4, 101, "2023-02-05", 150)
]
customers = spark.createDataFrame(
data_customers, ["customer_id", "customer_name", "country"])
orders = spark.createDataFrame(
data_orders, ["order_id", "customer_id", "order_date", "total_amount"])
from pyspark.sql import functions as sf
customers.alias("c").where(
orders.alias("o").where(
sf.col("o.customer_id") == sf.col("c.customer_id").outer()
).exists()
).orderBy("customer_id").show()
# +-----------+-------------+-------+
# |customer_id|customer_name|country|
# +-----------+-------------+-------+
# | 101| Alice| USA|
# | 102| Bob| Canada|
# | 103| Charlie| USA|
# +-----------+-------------+-------+
customers.alias("c").where(
~orders.alias("o").where(
sf.col("o.customer_id") == sf.col("c.customer_id").outer()
).exists()
).orderBy("customer_id").show()
# +-----------+-------------+---------+
# |customer_id|customer_name| country|
# +-----------+-------------+---------+
# | 104| David|Australia|
# +-----------+-------------+---------+