Skip to main content

Celonis Product Documentation

LEAD
Description

LEAD returns the row that follows the current row by offset number of rows. It is possible to specify a column based ordering and partitioning. Null values are skipped.

Syntax
  LEAD ( table.column [, ORDER BY ( order_column [sorting], ... )] [, PARTITION BY ( partition_column, ... )] [, offset ] )
 
  • column: The source column where following rows are taken from.

  • order_column: Optional order column to specify an sorting.

  • sorting: Each of these columns can have an optional tag specifying the ordering of the column. Default is ascending:

    • ASC: Ascending order

    • DESC: Descending order

  • partition_column: Optional partition column to specify groups in which LEAD should operate.

  • offset: The number of non-NULL rows following the current row. The default value is 1.

Examples

[1]

Simple example of LEAD returning the following row. If a row does not have a following row NULL is returned.

Query

Column1

         LEAD ( "Table1"."column" )
        

Input

Output

Table1

column : string

'C'

'D'

'A'

'E'

'B'

Result

Column1 : string

'D'

'A'

'E'

'B'

null

[2]

LEAD with an offset.

Query

Column1

         LEAD ( "Table1"."column" , 3 )
        

Input

Output

Table1

column : string

'C'

'D'

'A'

'E'

'B'

Result

Column1 : string

'E'

'B'

null

null

null

Ordering

One or more columns can be given to specify an ordering. This tells the LEAD function what the following element actually is. Optionally every column can be tagged as ascending or descending.

[3]

LEAD with a single order column.

Query

Column1

         LEAD ( "Table1"."column" , ORDER BY ( "Table1"."order" ) )
        

Input

Output

Table1

column : string

order : int

'A'

3

'B'

2

'C'

5

'D'

1

'E'

4

Result

Column1 : string

'E'

'A'

null

'B'

'C'

[4]

LEAD with a single order column tagged as descending.

Query

Column1

         LEAD ( "Table1"."column" , ORDER BY ( "Table1"."order" DESC ) )
        

Input

Output

Table1

column : string

order : int

'A'

3

'B'

2

'C'

5

'D'

1

'E'

4

Result

Column1 : string

'B'

'D'

'E'

null

'A'

[5]

LEAD with multiple order columns.

Query

Column1

         LEAD ( "Table1"."column" , ORDER BY ( "Table1"."order1" , "Table1"."order2" ) )
        

Input

Output

Table1

column : string

order1 : int

order2 : string

'A'

3

'z'

'B'

1

'z'

'C'

1

'y'

'D'

2

'y'

'E'

2

'x'

Result

Column1 : string

null

'E'

'B'

'A'

'D'

Partitioning

The partition columns specify groups. The LEAD function operates independently within every group. This means when an ordering is given it is applied within every group and the last offset elements in every group have a leading value of null.

[6]

LEAD with a single partition column.

Query

Column1

         LEAD ( "Table1"."column" , PARTITION BY ( "Table1"."Country" ) )
        

Input

Output

Table1

column : int

Country : string

1

'Germany'

2

'Germany'

3

'Germany'

1

'USA'

2

'USA'

Result

Column1 : int

2

3

null

2

null

[7]

LEAD with multiple partition columns.

Query

Column1

         LEAD ( "Table1"."column" , PARTITION BY ( "Table1"."Country" , "Table1"."State" ) )
        

Input

Output

Table1

column : int

Country : string

State : string

1

'Germany'

'Bavaria'

2

'Germany'

'Berlin'

2

'Germany'

'Bavaria'

3

'Germany'

'Bavaria'

1

'USA'

'California'

Result

Column1 : int

2

null

3

null

null

[8]

LEAD with multiple partition columns.

Query

Column1

         LEAD ( "Table1"."column" , ORDER BY ( "Table1"."year" ) , PARTITION BY ( "Table1"."Country" , "Table1"."State" ) )
        

Input

Output

Table1

column : int

year : date

Country : string

State : string

1

Tue Dec 31 2002 00:00:00.000

'Germany'

'Bavaria'

2

Fri Dec 31 1999 00:00:00.000

'Germany'

'Berlin'

2

Mon Dec 31 2001 00:00:00.000

'Germany'

'Bavaria'

3

Fri Dec 31 1999 00:00:00.000

'Germany'

'Bavaria'

1

Fri Dec 31 1999 00:00:00.000

'USA'

'California'

Result

Column1 : int

null

null

1

2

null

NULL handling

The leading value for a NULL value is the same value as the leading value of the last non-NULL value. The offset parameter counts only non-NULL values.

[9]

Example for LEAD applied to a column including NULL values.

Query

Column1

         LEAD ( "Table1"."column" , PARTITION BY ( "Table1"."Country" ) )
        

Input

Output

Table1

column : int

Country : string

1

'Germany'

null

'Germany'

3

'Germany'

4

'Germany'

1

'USA'

2

'USA'

null

'USA'

Result

Column1 : int

3

3

4

null

2

null

null

[10]

Offset ignores NULL values.

Query

Column1

         LEAD ( "Table1"."column" , PARTITION BY ( "Table1"."Country" ) , 2 )
        

Input

Output

Table1

column : int

Country : string

1

'Germany'

null

'Germany'

3

'Germany'

4

'Germany'

1

'USA'

2

'USA'

null

'USA'

null

'USA'

5

'USA'

Result

Column1 : int

4

4

null

null

5

null

null

null

null

Advanced Examples

[11]

LEAD can be used to simulate ACTIVITY_LEAD.

Query

Column1

         LEAD ( "Table1"."activity" , ORDER BY ( "Table1"."timestamp" ) , PARTITION BY ( "Table1"."case" ) )
        

Input

Output

Table1

case : int

activity : string

timestamp : date

1

'A'

Mon Feb 01 2016 01:00:00.000

1

'B'

Mon Feb 01 2016 02:00:00.000

1

'C'

Mon Feb 01 2016 03:00:00.000

2

'A'

Mon Feb 01 2016 01:00:00.000

2

'B'

Mon Feb 01 2016 02:00:00.000

2

'C'

Mon Feb 01 2016 03:00:00.000

2

'D'

Mon Feb 01 2016 04:00:00.000

Result

Column1 : string

'B'

'C'

null

'B'

'C'

'D'

null

[12]

It is possible to combine an arbitrary amount of order columns with an arbitrary amount of partition columns.

Query

Column1

         LEAD ( "Table1"."column" , ORDER BY ( "Table1"."year" DESC , "Table1"."value" ) , PARTITION BY ( "Table1"."Country" , "Table1"."State" ) )
        

Input

Output

Table1

column : int

year : date

value : float

Country : string

State : string

1

Tue Dec 31 2002 00:00:00.000

1.0

'Germany'

'Bavaria'

2

Fri Dec 31 1999 00:00:00.000

2.0

'Germany'

'Berlin'

2

Mon Dec 31 2001 00:00:00.000

3.0

'Germany'

'Bavaria'

3

Fri Dec 31 1999 00:00:00.000

4.0

'Germany'

'Bavaria'

1

Fri Dec 31 1999 00:00:00.000

5.0

'USA'

'California'

1

Sun Dec 31 2000 00:00:00.000

6.0

'Germany'

'Berlin'

5

Tue Dec 31 2002 00:00:00.000

7.0

'USA'

'California'

6

Mon Dec 31 2001 00:00:00.000

8.0

'Germany'

'Berlin'

4

Tue Dec 31 2002 00:00:00.000

9.0

'Germany'

'Bavaria'

7

Wed Dec 31 2003 00:00:00.000

10.0

'USA'

'California'

Result

Column1 : int

4

null

3

null

null

2

1

1

2

5

[13]

All optional parameters can be combined.

Query

Column1

         LEAD ( "Table1"."column" , ORDER BY ( "Table1"."year" DESC , "Table1"."value" ) , PARTITION BY ( "Table1"."Country" , "Table1"."State" ) , 2 )
        

Input

Output

Table1

column : int

year : date

value : float

Country : string

State : string

1

Tue Dec 31 2002 00:00:00.000

1.0

'Germany'

'Bavaria'

2

Fri Dec 31 1999 00:00:00.000

2.0

'Germany'

'Berlin'

2

Mon Dec 31 2001 00:00:00.000

3.0

'Germany'

'Bavaria'

3

Fri Dec 31 1999 00:00:00.000

4.0

'Germany'

'Bavaria'

1

Fri Dec 31 1999 00:00:00.000

5.0

'USA'

'California'

1

Sun Dec 31 2000 00:00:00.000

6.0

'Germany'

'Berlin'

5

Tue Dec 31 2002 00:00:00.000

7.0

'USA'

'California'

6

Mon Dec 31 2001 00:00:00.000

8.0

'Germany'

'Berlin'

4

Tue Dec 31 2002 00:00:00.000

9.0

'Germany'

'Bavaria'

7

Wed Dec 31 2003 00:00:00.000

10.0

'USA'

'California'

Result

Column1 : int

2

null

null

null

null

null

null

2

3

1

Use Cases
See also: