2016-09-23 1 views

Antwort

2

Dies ist mit Legacy-SQL-Aggregatfunktionen nicht möglich, aber es ist möglich, mit standard SQL und seinen Aggregatfunktionen STRING_AGG (entspricht GROUP_CONCAT in Legacy-SQL) und ARRAY_AGG (entspricht NEST). Beide Funktionen unterstützen optional LIMIT Klausel wie in "Using LIMIT with aggregate functions" dokumentiert.

Zum Beispiel:

select string_agg(x LIMIT 2) 
from unnest(['hello', 'world!', 'foo', 'bar', 'baz']) x 

kehrt 'hello,world!' Zeichenfolge und

select array_agg(x LIMIT 2) 
from unnest(['hello', 'world!', 'foo', 'bar', 'baz']) x 

kehrt ['hello', 'world!'] Array.

0

Für Legacy SQL

SELECT GROUP_CONCAT_UNQUOTED(x) FROM (
    SELECT x FROM 
    (SELECT 'hello' AS x), 
    (SELECT 'world!' AS x), 
    (SELECT 'foo' AS x), 
    (SELECT 'bar' AS x), 
    (SELECT 'baz' AS x), 
LIMIT 2 
) 

und

SELECT NEST(x) FROM (
    SELECT x FROM 
    (SELECT 'hello' AS x), 
    (SELECT 'world!' AS x), 
    (SELECT 'foo' AS x), 
    (SELECT 'bar' AS x), 
    (SELECT 'baz' AS x), 
LIMIT 2 
) 

sie jeweils 'hello,world!' String und ['hello', 'world!'] "array" return

unten ist beispielsweise mit GROUP BY

SELECT id, GROUP_CONCAT_UNQUOTED(x) FROM (
    SELECT id, x, ROW_NUMBER() OVER(PARTITION BY id) AS num FROM 
    (SELECT 1 AS id, 'hello' AS x), 
    (SELECT 1 AS id, 'world!' AS x), 
    (SELECT 1 AS id, 'foo' AS x), 
    (SELECT 1 AS id, 'bar' AS x), 
    (SELECT 1 AS id, 'baz' AS x), 
    (SELECT 2 AS id, 'hello2' AS x), 
    (SELECT 2 AS id, 'world2!' AS x), 
    (SELECT 2 AS id, 'foo2' AS x), 
    (SELECT 2 AS id, 'bar2' AS x), 
    (SELECT 2 AS id, 'baz2' AS x), 
) 
WHERE num < 3 
GROUP BY id 
+0

Diese Lösung funktioniert jedoch nicht mit GROUP BY, das Teil der ursprünglichen Frage war (aber Beispiele zur Vereinfachung haben es weggelassen). –

+0

Beispiel mit 'GROUP BY' hinzugefügt –

Verwandte Themen