Description:
Compute each member of a sequence according to the specified condition to generate multiple records and concatenate them into a new table sequence.
Syntax:
A.news(X;xi:Fi,…)
Note:
The function computes expression xi on each member of sequence A and sequence/integer X, generate multiple records and concatenate them to generate a new table sequence.
Parameter:
A |
A sequence. |
X |
A sequence/integer; can be understood as to(X) when it is an integer, meaning performing X number of computations on sequence A. |
xi |
An expression, whose results will be field values; the sign ~ used in the parameter references data from X instead of A. |
Fi |
Field name of the result sequence; when omitted, field names are xi by default, and use the original field names when xi is #i. |
Option:
@1 |
Left join, which creates an empty record when X is empty; here it is number 1 instead of letter l. |
@m |
Use parallel processing to increase performance. |
@q |
Return a sequence of sequences when there is only one parameter xi and parameter Fi is absent and when the result of computing xi is a sequence. |
Return value:
Table sequence
Example:
When A is a sequence
|
A |
|
1 |
[1,2,3,4,5] |
|
2 |
=A1.news([10,20]; A1.~:a,~:b,a*b) |
Parameter X is a sequence and compute members of A and X one by one, that is, use A1’s members as values of field a, members of [10,20] as values of field b and result values of a*b as values of the 3rd field; as parameter Fi is absent, use parameter xi as field names.
|
3 |
=A1.news(2; A1.~:a, ~:b,a*b) |
Parameter X is an integer, which is equivalent to to(2), and compute each of A1’s member two times.
|
When A is a table sequence
|
A |
|
1 |
=demo.query("select top 5 ID,NAME,BIRTHDAY,DEPT,SALARY from EMPLOYEE") |
|
2 |
=A1.news(~;NAME,age(BIRTHDAY):AGE) |
Parameter X is a table sequence and symbol ~ is understood as member of A1; compute A1 and return records consisting of NAME field and AGE field, where AGE field is computed from A1’s BIRTHDAY field.
|
3 |
=A1.news(3;EID,NAME,SALARY*~:Salary) |
Parameter X is integer 3 and compute each member of A1 three times; ~ is understood as the current number of computations.
|
When A is a record sequence
|
A |
|
1 |
=demo.query("select top 10 EID,NAME,DEPT,GENDER,SALARY from EMPLOYEE") |
|
2 |
=A1.group(DEPT).(~.group(GENDER)) |
Group table sequence A1 by DEPT and then group the result by GENDER.
|
3 |
=A2.news(~;A2.~.DEPT,A2.~.~.GENDER,A2.~.~.avg(SALARY):AvgSalary) |
Get DEPT and GENDER values from A2’s group and calculate average salary of each GENDER in each DEPT.
|
When parameter xi uses #i
|
A |
|
1 |
=demo.query("select top 10 EID,NAME,BIRTHDAY,GENDER,SALARY from EMPLOYEE") |
|
2 |
=A1.group(GENDER;~:GenderGroup,~.avg(SALARY):Avg) |
Group A1 by GENDER and calculate average salary in each group.
|
3 |
=A2.news(GenderGroup;EID,#2,GENDER, age(~.BIRTHDAY):age,SALARY+1000:Nsalary,A2.Avg:AvgSalary) |
Compute the expression on each group of A2 and generate records to form a new table sequence, during which #2 represents NAME, the 2nd field of GenderGroup.
|
Use @1 option to perform left join
|
A |
|
1 |
=demo.query("select top 5 EID,NAME,BIRTHDAY,GENDER,SALARY from EMPLOYEE") |
|
2 |
=A1.group(GENDER;~:GenderGroup) |
Group A1’s records by GENDER.
|
3 |
=demo.query("select top 10 EID,NAME,BIRTHDAY,GENDER,SALARY from EMPLOYEE") |
|
4 |
=A3.group(GENDER;avg(SALARY):avg) |
Group A3 by GENDER and calculate average salary in each group.
|
5 |
=A4.join(GENDER,A2:GENDER,GenderGroup) |
Perform association between A2 and A4 according to GENDER field; since A2 does not have records where GENDER is M, the corresponding GenderGroup field values are represented by nulls after table association.
|
6 |
=A5.news(GenderGroup;EID,NAME,GENDER,age(~.BIRTHDAY):age,avg:AvgSalary) |
Compute the expression on record sequence A5 to generate new records, from which those whose GenderGroup is empty are by default discarded.
|
7 |
=A5.news@1(GenderGroup;EID,NAME,GENDER,age(~.BIRTHDAY):age,avg:AvgSalary) |
Use @1 option to create an empty record when record sequence X is empty
|
Use @q option to return a sequence of sequences:
|
A |
|
1 |
=file("emp.txt").cursor@w().fetch() |
Return data as follows:
|
2 |
=A1.news@q(2;[A1.~(2),A1.~(3)*~]) |
With @q option, return a sequence of sequences as there is only one parameter xi and parameter Fi is absent and since the result of computing xi is a sequence:
|
Related function: