

 从补丁 198 开始，Amazon Redshift 将不再支持创建新的 Python UDF。现有的 Python UDF 将继续正常运行至 2026 年 6 月 30 日。有关更多信息，请参阅[博客文章](https://www.amazonaws.cn/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/)。

# 示例：在 Redshift Spectrum 中执行相关的子查询
<a name="c_performing-correlated-subqueries-spectrum"></a>

本主题介绍如何在 Redshift Spectrum 中执行关联子查询。关联子查询是使用来自外部查询的值的查询。

您可以在 Redshift Spectrum 中执行相关的子查询。`$spectrum_oid` 伪列提供了使用 Redshift Spectrum 执行相关查询的功能。要执行关联的子查询，必须启用伪列 `$spectrum_oid`，但它不会显示在 SQL 语句中。有关更多信息，请参阅 [Pseudocolumns](c-spectrum-external-tables.md#c-spectrum-external-tables-pseudocolumns)。

要为此示例创建外部架构和外部表，请参阅[Amazon Redshift Spectrum 入门](c-getting-started-using-spectrum.md)。

以下是 Redshift Spectrum 中相关子查询的示例。

```
select *
from myspectrum_schema.sales s
where exists
( select *
from myspectrum_schema.listing l
where l.listid = s.listid )
order by salesid
limit 5;
```

```
salesid  listid   sellerid   buyerid   eventid   dateid  qtysold  pricepaid   commission   saletime
1        1        36861      21191     7872      1875    4        728         109.2        2008-02-18 02:36:48
2        4        8117       11498     4337      1983    2        76          11.4         2008-06-06 05:00:16	
3        5        1616       17433     8647      1983    2        350         52.5         2008-06-06 08:26:17	
4        5        1616       19715     8647      1986    1        175         26.25        2008-06-09 08:38:52	
5        6        47402      14115     8240      2069    2        154         23.1         2008-08-31 09:17:02
```