跳到主要内容
翻译页面

本页面由 AI 辅助从英文翻译而来。如两者存在差异,以英文版本为准。

SELECT

返回资源的一个或多个实例。

另请参阅: [ StackQL 资源层次结构 ] [WITH (CTEs)] [窗口函数]


语法​

selectStatement::=

withClauseSELECTDISTINCT'*'fullFieldNameASaliasfunctionCallASaliaswindowFunctionCallASaliasFROMresourceOUTERJOINresourceONexpressionWHEREexpressionGROUPBYgroupByItem','groupByItemWITHROLLUPHAVINGexpressionORDERBYfullFieldName','fullFieldNameASCDESCLIMITnumber';'

 
 

[ WITH [ RECURSIVE ] <cteName> [ ( <columnList> ) ] AS ( <selectStatement> ) [, ...] ]
SELECT { * | <fieldList> | <windowFunctionCall> [ AS <alias> ] }
FROM { <multipartIdentifier> | <joinStatement(s)> }
[ WHERE <expression> ]
[ GROUP BY <fieldList> ]
[ HAVING <expression> ]
[ WINDOW <windowName> AS ( <windowSpec> ) [, ...] ]
[ ORDER BY <fieldList> [ ASC | DESC ] ]
[ LIMIT <integer> ]
[ UNION <selectStatement> ];

不带 ON 子句的 JOIN 被视为 CROSS JOIN,因此连接条件可以改在 WHERE 子句中给出。v0.12.732 之前的 StackQL 发布版本无法规划不带条件的 JOIN。


示例​

基本 SELECT 语句​

在已通过身份验证的会话中运行基本 SELECT 语句。

-- Selecting all resources deployed within a service using a basic SELECT statement
SELECT * FROM google.compute.instances
WHERE project = 'stackql-demo'
AND zone = 'australia-southeast1-a';

使用 SELECT 语句返回指定字段​

运行带列列表的 SELECT 语句,返回 Compute Engine 实例的指定字段。

-- Selecting specified fields from a resource
SELECT id, name
FROM google.compute.instances
WHERE project = 'stackql-demo'
AND zone = 'australia-southeast1-a';

使用 ORDER BY 子句返回按某列升序排序的结果集​

运行 SELECT 语句,返回 Cloud Storage buckets 资源的字段,并按升序(最早的在前)对结果排序。

-- Order a list of Cloud Storage buckets by creation time (in ascending order)
SELECT name, location, timeCreated
FROM google.storage.buckets WHERE project = 'stackql'
ORDER BY timeCreated;

使用 ORDER BY 子句返回按某列降序排序的结果集​

运行 SELECT 语句,返回 Cloud Storage buckets 资源的字段,并按降序(最新的在前)对结果排序。

-- Order a list of Cloud Storage buckets by creation time (in descending order)
SELECT name, location, timeCreated
FROM google.storage.buckets WHERE project = 'stackql'
ORDER BY timeCreated DESC;

使用 ORDER BY 和 LIMIT 子句获取某列的首个结果​

运行 SELECT 语句,返回最近创建的 Cloud Storage 存储桶。

-- Find the most recently created bucket
SELECT name, location, timeCreated
FROM google.storage.buckets WHERE project = 'stackql'
ORDER BY timeCreated DESC LIMIT 1;

将 COUNT 函数与 GROUP BY 和 HAVING 子句配合使用,按分组和过滤条件统计资源数量​

运行带 COUNT 函数的 SELECT 语句,使用 GROUP BY 和 HAVING 子句返回除亚洲之外每个位置的存储桶数量。

-- Return a count of the number of buckets in every location other than Asia
SELECT name, location, count(*) as num_buckets
FROM google.storage.buckets WHERE project = 'stackql'
GROUP BY location
HAVING location != 'ASIA';

有关 COUNT 函数以及 StackQL 支持的其他聚合函数的更多信息,请参阅聚合函数。

从 Azure VM Scaleset 获取按需 VM 及其对应网络接口的列表​

运行 SELECT 语句,使用 SPLIT_PART 和 JSON_EXTRACT 函数获取 OD VM 及其 NIC 的列表。注意,在不带限定符的情况下,subscription_id 和 resource_group_name 会应用于全部三个资源。

SELECT c.name AS vm_name,
SPLIT_PART(JSON_EXTRACT(c.properties,'$.networkProfile.networkInterfaces[0].id'), '/', -1) AS nic_name
FROM
azure.compute.virtual_machine_scale_sets a
INNER JOIN azure.compute.virtual_machine_scale_set_vms b
ON a.name = b.virtualMachineScaleSetName
INNER JOIN azure.compute.virtual_machines c
ON b.name = c.name
WHERE subscription_id = '0123456789'
AND resource_group_name = 'vmss-flex'
AND JSON_EXTRACT(c.properties,'$.priority') is null;

有关窗口函数的更多信息,请参阅窗口函数。

通过 WITH 使用公用表表达式(CTE)​

CTE 可用于定义临时的命名结果集,这些结果集可在主查询中引用。

-- Combine and rank contributors across multiple repositories
WITH all_contributors AS (
SELECT login, contributions, html_url
FROM github.repos.contributors
WHERE owner = 'stackql' AND repo = 'stackql'
UNION ALL
SELECT login, contributions, html_url
FROM github.repos.contributors
WHERE owner = 'stackql' AND repo = 'stackql-deploy'
UNION ALL
SELECT login, contributions, html_url
FROM github.repos.contributors
WHERE owner = 'stackql' AND repo = 'stackql-provider-registry'
)
SELECT
DENSE_RANK() OVER (ORDER BY SUM(contributions) DESC) as rank,
login,
SUM(contributions) as total_contributions,
MAX(html_url) as html_url
FROM all_contributors
GROUP BY login
ORDER BY total_contributions DESC;

有关 CTE 的更多信息,请参阅 WITH (CTEs)。