MySQL 支持对一组值执行计算的聚合函数。有关这些函数的一般信息,请参见 第 14.19.1 节,“聚合函数描述”。本节描述了 ST_Collect() 空间聚合函数。MySQL supports aggregate functions that perform a calculation on a
set of values. For general information about these functions, see
Section 14.19.1, “Aggregate Function Descriptions”. This section describes the
ST_Collect() spatial aggregate
function.
ST_Collect() 可以作为窗口函数使用,其语法描述中的 [ 表示可选的 over_clause]OVER 子句。over_clause 在 第 14.20.2 节,“窗口函数概念与语法” 中有描述,该部分还包括关于窗口函数使用的其他信息。ST_Collect() can be used as a
window function, as signified in its syntax description by
[,
representing an optional over_clause]OVER clause.
over_clause is described in
Section 14.20.2, “Window Function Concepts and Syntax”, which also includes
other information about window function usage.
ST_Collect([DISTINCT]g) [over_clause]聚合几何值并返回一个单一的几何集合值。使用
DISTINCT选项时,返回不同几何参数的聚合结果。Aggregates geometry values and returns a single geometry collection value. With theDISTINCToption, returns the aggregation of the distinct geometry arguments.与其他聚合函数一样,可以使用
GROUP BY将参数分组为子集。ST_Collect()为每个子集返回一个聚合值。As with other aggregate functions,GROUP BYmay be used to group arguments into subsets.ST_Collect()returns an aggregate value for each subset.如果存在
over_clause,此函数将作为窗口函数执行。over_clause的描述如第 14.20.2 节,“窗口函数的概念和语法”所述。与大多数支持窗口的聚合函数相比,ST_Collect()允许over_clause与DISTINCT同时使用。This function executes as a window function ifover_clauseis present.over_clauseis as described in Section 14.20.2, “Window Function Concepts and Syntax”. In contrast to most aggregate functions that support windowing,ST_Collect()permits use ofover_clausetogether withDISTINCT.ST_Collect()处理其参数的方式如下:ST_Collect()handles its arguments as follows:NULL参数将被忽略。NULLarguments are ignored.如果所有参数都是
NULL或聚合结果为空,则返回值为NULL。If all arguments areNULLor the aggregate result is empty, the return value isNULL.如果任何几何参数不是语法上格式正确的几何体,则会发生
ER_GIS_INVALID_DATA错误。If any geometry argument is not a syntactically well-formed geometry, anER_GIS_INVALID_DATAerror occurs.如果任何几何参数是在未定义空间参考系统(SRS)中的语法上良构的几何,将会发生
ER_SRS_NOT_FOUND错误。If any geometry argument is a syntactically well-formed geometry in an undefined spatial reference system (SRS), anER_SRS_NOT_FOUNDerror occurs.如果有多个几何参数且这些参数在相同的 SRS 中,则返回值也在该 SRS 中。如果这些参数不在相同的 SRS 中,则会发生
ER_GIS_DIFFERENT_SRIDS_AGGREGATION错误。If there are multiple geometry arguments and those arguments are in the same SRS, the return value is in that SRS. If those arguments are not in the same SRS, anER_GIS_DIFFERENT_SRIDS_AGGREGATIONerror occurs.结果是可能的最窄的
Multi或XxxGeometryCollection值,结果类型由非NULL几何参数确定,如下所示:The result is the narrowestMultiorXxxGeometryCollectionvalue possible, with the result type determined from the non-NULLgeometry arguments as follows:如果所有参数都是
Point值,则结果是MultiPoint值。If all arguments arePointvalues, the result is aMultiPointvalue.如果所有参数都是
LineString值,则结果是MultiLineString值。If all arguments areLineStringvalues, the result is aMultiLineStringvalue.如果所有参数都是
Polygon值,则结果是MultiPolygon值。If all arguments arePolygonvalues, the result is aMultiPolygonvalue.否则,参数是不同类型的几何图形,结果是
GeometryCollection值。Otherwise, the arguments are a mix of geometry types and the result is aGeometryCollectionvalue.
此示例数据集显示了按年份和制造地点划分的假设产品:This example data set shows hypothetical products by year and location of manufacture:
CREATE TABLE product ( year INTEGER, product VARCHAR(256), location Geometry ); INSERT INTO product (year, product, location) VALUES (2000, "Calculator", ST_GeomFromText('point(60 -24)',4326)), (2000, "Computer" , ST_GeomFromText('point(28 -77)',4326)), (2000, "Abacus" , ST_GeomFromText('point(28 -77)',4326)), (2000, "TV" , ST_GeomFromText('point(38 60)',4326)), (2001, "Calculator", ST_GeomFromText('point(60 -24)',4326)), (2001, "Computer" , ST_GeomFromText('point(28 -77)',4326));使用
ST_Collect()对数据集进行的一些示例查询:Some sample queries usingST_Collect()on the data set:mysql>
SELECT ST_AsText(ST_Collect(location)) AS resultFROM product;+------------------------------------------------------------------+ | result | +------------------------------------------------------------------+ | MULTIPOINT((60 -24),(28 -77),(28 -77),(38 60),(60 -24),(28 -77)) | +------------------------------------------------------------------+ mysql>SELECT ST_AsText(ST_Collect(DISTINCT location)) AS resultFROM product;+---------------------------------------+ | result | +---------------------------------------+ | MULTIPOINT((60 -24),(28 -77),(38 60)) | +---------------------------------------+ mysql>SELECT year, ST_AsText(ST_Collect(location)) AS resultFROM product GROUP BY year;+------+------------------------------------------------+ | year | result | +------+------------------------------------------------+ | 2000 | MULTIPOINT((60 -24),(28 -77),(28 -77),(38 60)) | | 2001 | MULTIPOINT((60 -24),(28 -77)) | +------+------------------------------------------------+ mysql>SELECT year, ST_AsText(ST_Collect(DISTINCT location)) AS resultFROM product GROUP BY year;+------+---------------------------------------+ | year | result | +------+---------------------------------------+ | 2000 | MULTIPOINT((60 -24),(28 -77),(38 60)) | | 2001 | MULTIPOINT((60 -24),(28 -77)) | +------+---------------------------------------+ # selects nothing mysql>SELECT ST_Collect(location) AS resultFROM product WHERE year = 1999;+--------+ | result | +--------+ | NULL | +--------+ mysql>SELECT ST_AsText(ST_Collect(location)OVER (ORDER BY year, product ROWS BETWEEN 1 PRECEDING AND CURRENT ROW))AS resultFROM product;+-------------------------------+ | result | +-------------------------------+ | MULTIPOINT((28 -77)) | | MULTIPOINT((28 -77),(60 -24)) | | MULTIPOINT((60 -24),(28 -77)) | | MULTIPOINT((28 -77),(38 60)) | | MULTIPOINT((38 60),(60 -24)) | | MULTIPOINT((60 -24),(28 -77)) | +-------------------------------+