2

是来自 aws 网站的片段:

WITH dataset AS (
  SELECT ARRAY[
    CAST(
      ROW('aws.amazon.com', ROW(true)) AS ROW(hostname VARCHAR, flaggedActivity ROW(isNew BOOLEAN))
    ),
    CAST(
      ROW('news.cnn.com', ROW(false)) AS ROW(hostname VARCHAR, flaggedActivity ROW(isNew BOOLEAN))
    ),
    CAST(
      ROW('netflix.com', ROW(false)) AS ROW(hostname VARCHAR, flaggedActivity ROW(isNew BOOLEAN))
    )
  ] as items
)
SELECT sites.hostname, sites.flaggedActivity.isNew
FROM dataset, UNNEST(items) t(sites)
WHERE sites.flaggedActivity.isNew = true;

它有效!但是什么t(sites)意思?当我尝试使用真实表而不是dataset出现错误时Table 'site' not found

它看起来很奇怪 - 类似于使用UNNEST关键字调用的函数。有人可以解释这是什么吗?

4

1 回答 1

5
FROM dataset, UNNEST(items) t(sites)

UNNEST() is a set returning-function: it produces a series of rows, with one column holding the values from the original array.

This set of rows is also called a derived table. Here t is an alias for the derived table, and site is the name of the (only) column in this derived table.

It might be easier to understand if we add the AS keyword:

FROM dataset, UNNEST(items) AS t(sites)
于 2020-06-25T16:50:27.093 回答