| --- |
| { |
| "title": "FOREACH", |
| "language": "en" |
| } |
| --- |
| |
| <!-- |
| Licensed to the Apache Software Foundation (ASF) under one |
| or more contributor license agreements. See the NOTICE file |
| distributed with this work for additional information |
| regarding copyright ownership. The ASF licenses this file |
| to you under the Apache License, Version 2.0 (the |
| "License"); you may not use this file except in compliance |
| with the License. You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, |
| software distributed under the License is distributed on an |
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| KIND, either express or implied. See the License for the |
| specific language governing permissions and limitations |
| under the License. |
| --> |
| |
| ## FOREACH |
| |
| ### description |
| #### Syntax |
| |
| `AGGREGATE_FUNCTION_FOREACH(arg...)` |
| Converts an aggregate function for tables into an aggregate function for arrays that aggregates the corresponding array items and returns an array of results. For example, sum_foreach for the arrays [1, 2], [3, 4, 5]and[6, 7]returns the result [10, 13, 5] after adding together the corresponding array items. |
| |
| |
| |
| |
| ### example |
| ``` |
| mysql [test]>select a , s from db; |
| +-----------+---------------+ |
| | a | s | |
| +-----------+---------------+ |
| | [1, 2, 3] | ["ab", "123"] | |
| | [20] | ["cd"] | |
| | [100] | ["efg"] | |
| | NULL | NULL | |
| | [null, 2] | [null, "c"] | |
| +-----------+---------------+ |
| |
| mysql [test]>select sum_foreach(a) from db; |
| +----------------+ |
| | sum_foreach(a) | |
| +----------------+ |
| | [121, 4, 3] | |
| +----------------+ |
| |
| mysql [test]>select count_foreach(s) from db; |
| +------------------+ |
| | count_foreach(s) | |
| +------------------+ |
| | [3, 2] | |
| +------------------+ |
| |
| mysql [test]>select array_agg_foreach(a) from db; |
| +-----------------------------------+ |
| | array_agg_foreach(a) | |
| +-----------------------------------+ |
| | [[1, 20, 100, null], [2, 2], [3]] | |
| +-----------------------------------+ |
| |
| mysql [test]>select map_agg_foreach(a,a) from db; |
| +---------------------------------------+ |
| | map_agg_foreach(a, a) | |
| +---------------------------------------+ |
| | [{1:1, 20:20, 100:100}, {2:2}, {3:3}] | |
| +---------------------------------------+ |
| ``` |
| ### keywords |
| FOREACH |