# Loop Computations ## 1.     Compare tables row by row to find the equals

Perform judgements by loop, adding a new member to an existing sequence each time.

【Example 1】Compare two files that have same number of rows to count the rows with same data. Below is part of the source data:

【SPL script】

Below is A5’s result:

## 2.     Assign values by loop

Loop through members of a set to compute each and assign value to it.

【Example 2】According to the following sales table, give a  reward of sales amount’s 5% to each salesperson whose performance in 2014 rank the top 10%. Below is part of the source table:

【SPL script】

Below is A5’ s result:

## 3.     Complex inter-row computation

After data is grouped, summarize specific columns to get different aggregates while performing inter-row computations.

【Example 3】According to the user payment details table below, calculate the monthly sum payable in 2014 for each user. Below is part of the source table:

The target is to output the payable sum per month in the year of 2014. If no data exists for the current month, the payable sum will be the one in the previous month:

【SPL script】

Below is A2’s result:

## 4.     Get the largest number of consecutively rising days

Count the frequency of continuous rising of a specific column value during the loop.

【Example 4】According to the SSE Composite Index records below, get the closing price’s largest number of consecutive rising days in 2019. Below is the part of the source table:

【SPL script】

Below is A3’s result:

## 5.     Nested loop

Use loop functions in a nested way.

【Example 5】The hundred fowls problem: If a rooster is worth five coins, a hen three coins, and three chicks together are worth one coin, how many roosters, hens, and chicks totaling 100 can be bought for 100 coins?

【SPL script】

Below is A4’s result:

## 6.     Call loop number during the loop

Query a certain file by loop to generate desired information during which the loop number is displayed.

【Example 6】Query file 2 to output specific information according to key words in file 1.

【SPL script】

Below is A3’s result:

## 7.     Summarize data in skewness intervals starting from specific positions by loop

Calculate the average within skewness intervals starting from specific positions by loop.

【Example 7】According to the stock exchange table below, list the average closing price within 20 days for each transaction date during 1.1 – 1.10 in the year of 2020. Below is part of the source table:

【SPL script】

Below is A4’s result:

## 8.     Iterative accumulation

Perform iterative accumulation during the loop and then filtering according to the cumulative total.

【Example 8】According to the sales table below, count the days needed to bag 20 orders per month in the year of 2014. Below is part of the source table:

【SPL script】

Below is A4’s result:

## 9.     Post-grouping ranking

Get the rank in each group by loop.

【Example 9】According to the employee table below, get the rank of each employee’s salary in their department. Below is part of the source table:

【SPL script】

Below is A2’s result:

## 10.   Post-grouping tight ranking

Get the tight ranks for specific fields in each group by loop.

【Example 10】According to the scores table below, get the ranks of all subject scores for the student whose ID is 8 in class one. Below is part of the source table:

【SPL script】

Below is A4’s result:

## 11.   Iterative sum

Calculate iterative sums by loop.

【Example 11】According to the SSE Composite Index records below, calculate the cumulative amount total for each transaction date in 2019. Below is part of the source table:

【SPL script】

Below is A3’s result:

## 12.   User-defined iterative computation

Users define the computing expression and the desired termination condition for the iteration computation performed within the loop.

【Example 12】According to the sales table below, get the date when the sales target of 150,000 is achieved for the first quarter. Below is part of the source table:

【SPL script】

Below is A3’s result:

There are more examples in SPL CookBook.