Counting records

There are two main functions used to count records during load: RecNo() and RowNo(). After the data has been loaded, we can use another couple of interesting functions: FieldValueCount() and NoOfRows(). There is also a useful function, NoOfFields(), that tells us how many columns there are in a table.

RecNo

The RecNo() function gives us the number of the rows in the source table. While the output of the RecNo function will always be guaranteed to be ordered, there might be gaps in the sequence because rows may be excluded due to a where clause, for example, this load statement:

Table1:
Load *, RecNo() As RecNo1
Where Field1<>'C';
Load * Inline [
Field1
A
B
C
D
];

Only three rows will be loaded from the source because the row with C as a value is excluded by the Where clause. This results in this table:

Field1

RecNo1

A

1

B

2

D

4

The value 3 is missing in the sequence as the third row was not loaded.

It should also be noted that an additional load from a new source, even if it is concatenating to the same table, will have the numeric sequence restart at 1.

RowNo

The RowNo() function gives us the number of rows in the loaded in-memory table. There should be no gaps in the sequence because the next number is only assigned when the row is actually loaded. For example, if we replace the RecNo() function in the script in the previous example with RowNo(), we will get this result:

Field1

RowNo1

A

1

B

2

D

3

We have to watch out for one aspect of the RowNo() function when using the preceding loads. If we modified the preceding code like this:

Table1:
Load *, RowNo() As RowNo1
Where Field1<>'C';
Load *, RowNo() As RowNo2 Inline [
Field1
A
B
C
D
];

We will find that RowNo1 will have values as expected; however, RowNo2 will be all zeroes. This is because the RowNo() function only returns correctly in the top loader of a preceding load. It must be like this because each preceding load can have its own Where clause that can modify the number of rows loaded. Only at the topmost load do we actually know that a row is loaded.

RowNo() also differs from RecNo() because as it is the count of the number of rows actually loaded, additional concatenation of rows from different data sources does not reset the counter. So, if we had a couple of loads like this:

Table:
Load *, RecNo() As RecNo, RowNo() As RowNo
Inline [
Field
A
B
C
D
];

Load *, RecNo() As RecNo, RowNo() As RowNo
Where Field <> 'G';
Load *
Inline [
Field
E
F
G
H
];

The result would look like this:

Field

RecNo

RowNo

A

1

1

B

2

2

C

3

3

D

4

4

E

1

5

F

2

6

H

4

7

The RecNo() function as reset after the first load and skips the number for the excluded rows. The RowNo() sequence is unaffected by the fact of the second load.

FieldValueCount

The FieldValueCount function will return the number of values in a field. Be careful that it is not the number of rows in a table that contains the field, but it is the number of unique values in the field. The function takes the name of an existing field as a parameter; however, it needs to be passed as a string:

Let x=FieldValueCount('Field1'),

NoOfRows

The NoOfRows function returns the actual number of rows that have been loaded in a table. As with the previous function, the table name is passed as a string value:

Let x=NoOfRows('Table1'),

This function can actually be used inline during a table load. Logically, it will return RowNo()-1.

NoOfColumns

The NoOfColumns function is similar to the previous one except that it returns the number of columns. As before, we pass the table name as a string:

Let x=NoOfColumns('Table1'),

A use case for both of these table functions is to check whether the expected number of rows and columns are in a table after Join.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset