groupn()

Read(2459) Label: group,

Here’s how to use groupn() function.

cs.groupn(x;C)

Description:

Group records in a cursor and push them respectively into a sequence of channels.

Syntax:

cs.groupn(x;C)

Note:

The function groups records in cursor cs by grouping expression x, pushes them respectively into a sequence of channels, and returns the original cursor.

Parameter:

cs

Cursor records

x

Grouping expression

C

A sequence of channels

Return value:

Cursor

Example:

 

A

 

1

=demo.cursor("select NAME,GENDER,DEPT,BIRTHDAY from EMPLOYEE")

 

2

=channel()

Create a channel.

3

=channel()

Create a channel.

4

=A1.groupn(if(GENDER=="F",1,2);[A2,A3])

Group cursor records, and push one group where GENDER is F into A2’s channel and the other group into A3’s channel.

5

=A2.fetch()

Keep data in the channel in place.

6

=A3.fetch()

Keep data in the channel in place.

7

=A1.fetch()

 

8

=A2.result()

Get result from A2’s channel.

9

=A3.result()

Get result from A3’s channel.

ch.groupn( x;C )

Description:

Group records in channel ch and push them respectively into a sequence of channels.

Syntax:

ch.groupn(x;C)

Note:

The function groups records in channel ch by grouping expression x and pushes them respectively into a sequence of channels.

Parameter:

ch

Channel

x

Grouping expression

C

A sequence of channels

Return value:

A channel

Example:

 

A

 

1

=demo.cursor("select NAME,GENDER,DEPT,BIRTHDAY from EMPLOYEE")

 

2

=channel(A1)

Push cursor records into a sequence of channels and return a channel.

3

=channel()

Create a channel.

4

=channel()

Create a channel.

5

=A2.groupn(if(GENDER=="F",1,2);[A3,A4])

Group records of A2’s channel, and push one group where GENDER is F into A3’s channel and the other group into A4’s channel.

6

=A3.fetch()

Attach ch.fetch() function that gets the final result set to A3’s channel but keep data in A3’s channel in place

7

=A4.fetch()

Attach ch.fetch() function that gets the final result set to A4’s channel but keep data in A4’s channel in place.

8

=A1.fetch()

 

9

=A3.result()

Get result from A3’s channel.

10

=A4.result()

Get result from A4’s channel.

cs.groupn(x;F)

Description:

Attach the grouping record action to a cursor and return the original cursor while writing the grouped subsets to a sequence of bin files.

Syntax:

cs.groupn(x;F)

Note:

The function attaches a computation, which groups records of cursor cs by grouping expression x and writes the grouped subsets to sequence of bin files F, and returns the original cursor cs.

 

This is a delayed function.

Parameter:

cs

A cursor

x

Grouping expression

F

A sequence of bin file objects

Return value:

Cursor

Example:

 

A

 

1

=demo.cursor("select NAME,GENDER,DEPT,BIRTHDAY from EMPLOYEE")

Return a cursor.

2

=[file("f_dept.btx"),file("m_dept.btx")]

A sequence of bin file objects.

3

=A1.groupn(if(GENDER=="F",1,2);A2)

Attach a computation to cursor A1, which puts records where GENDER is F into the first group and the other records into the second group and writes the grouped subsets into a sequence of bin files in order, and return the original cursor.

4

=A3.skip()

Data will be really written to a bin file after the data fetching operation is really executed on cursor A1.

T.groupn(x;F)

Description:

Define a computation on a pseudo table, which will group its records and write the grouped subsets to a sequence of bin files.

Syntax:

T.groupn(x;F)

Note:

The function defines a computation on pseudo table T, which will group its records by grouping expression x and write the grouped subsets to a sequence of bin files F, and return a new pseudo table.

Parameter:

T

A pseudo table

x

Grouping expression

F

A sequence of bin file objects

Return value:

Pseudo table

Example:

 

A

 

1

=create(file).record(["empAll.ctx"])

 

2

=pseudo(A1)

Generate a pseudo table from the composite table.

3

=[file("f_dept.btx"),file("m_dept.btx")]

Return a sequence of bin file objects

4

=A2.groupn(if(GENDER=="F",1,2);A3)

Define a computation on A2’s pseudo table, which will put records where GENDER is F into the first group and the other records into the second group and write the grouped subsets into a sequence of bin files correspondingly.

5

=A4.cursor().fetch()

Fetch data from A4’s pseudo table, when the grouped subsets are actually written to bin files.