---
schema_version: "1"
id: e5f6dacd0e8ddd0e86676c09b4d6eda10bb83302df9921f3048beb37225d0e44
title: foreach
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/analytics/bi_dev_guide_saql/guides/reference/bi-saql-statement-foreach.md
canonical_url: https://developer.salesforce.com/docs/analytics/bi-dev-guide-saql/guide/bi-saql-statement-foreach.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 26e43d7cc9cc38738211d43fc6615ba91568f3000555870f21871bf55bab9fa0
products:
  - Analytics SAQL Developer Guide
  - analytics
---

# foreach

Applies a set of expressions to every row in a dataset. This action is often referred to as _projection_.

## Syntax

```nolang
q = foreach q generate expression as alias[, expression as alias ...];
```

The output column names are specified with the `as` keyword. The output data is ungrouped.

## Using foreach with Ungrouped Data

When used with ungrouped data, the `foreach` statement maps the input rows to output rows. The number of rows remains the same.

## Example

`a2 = foreach a1 generate carrier as carrier, miles as miles;`

## Using foreach with Grouped Data

When used with grouped data, the `foreach` statement behaves differently than it does with ungrouped data.

Fields can be directly accessed only when the value is the same for all group members. For example, the fields that were used as the grouping keys have the same value for all group members. Otherwise, use aggregate functions to access the members of a group. The type of the column determines which aggregate functions you can use. For example, if the column type is numeric, you can use the `sum()` function.

## Example

`z = foreach y generate day as day, unique(origin) as uorg, count() as n;`

## Using foreach with a case Expression

To create logic in a `foreach` statement that chooses between conditional statements, use a `case` expression.

## Projected Field Names

Each field name in a projection must be unique and not have the name 'none'. Invalid field names throw an error.

For example, the last line in this query is invalid because the same name is used for multiple projected fields:

```nolang
l = load "0Fabb000000002qCAA/0Fabb000000002WCAQ";
r = load "0Fcyy000000002qCAA/0Fcyy000000002WCAQ";
l = foreach l generate 'value'/'divisor' as 'value' , category as category;
r = foreach r generate 'value'/'divisor' as 'value' , category as category;
cg = cogroup l by category right, r by category;
cg = foreach cg generate r.category as 'category', sum(r.value) as sumrval, sum(l.value) as sumrval;
```

The following query is also invalid because the projected field name can't be 'none'.

```nolang
q = load "Products";
q = group q by all;
q = foreach q generate count() as 'none';
q = limit q 2000;
```

## Examples

For examples of projections, see [Calculate Grand Totals and Subtotals with the rollup Modifier and grouping() Function](https://developer.salesforce.com/docs/analytics/bi-dev-guide-saql/concept/bi-saql-examples-rollup-grouping.md).

**See Also**

- [Statements](https://developer.salesforce.com/docs/analytics/bi-dev-guide-saql/guide/bi-saql-statements.md)
