# Python vs. SPL 10 – One-to-N Association

In data analysis, we usually encounter the scenarios of associating two or more tables, and the association of tables can be divided into the following categories: one-to-one, one-to-many, many-to-one, and many-to-many. The association of one-to-one means that one record of a table corresponds to only one record of another table; the association of one-to-many indicates that one record of a table corresponds to multiple records of another table. This article will compare the computational abilities of Python and SPL in one-to-N association.

## Homo-dimensionassociation

One-to-one association is also known as homo dimension association, and the associated two tables are homo dimension tables to each other. For example:

Based on the final score table and the usual score table, calculate students’ scores of the semester (the percentages of final scores and usual scores are 0.7 and 0.3 respectively).

Final score table

Usual score table

Python

The merge function in Python can associate two Dataframes. Some students may lack a certain score, so we use outer join, then the score is recorded as 0 when there is no score; in this way, the final score can be obtained after weighed sum. After associating, a new Dataframe will be generated in Python, in which data are copied. In short, this whole processing takes up both a lot of time and plenty of memory.

SPL

The join function in SPL performs the association, @f option is outer join, and in A6 the final scores are calculated. The association in SPL only uses the primary key (studentid in this example) to create association on two tables and does not copy the data, so it occupies a little memory and performs very fast.

## Multiple homo-dimension tables

During associative calculation, we may often encounter scenarios where three or more homo-dimension tables need to be associated. For example:

Apart from the final scores and usual scores, students also have the scores of elective courses. And the percentages of the three are 0.6, 0.3, and 0.1 respectively.

The final score table and usual score table are the same as the previous example, and the elective score table is shown below:

Elective score table

Python

The merge function in Python can’t be used any longer for associating multiple homo-dimension tables because it only supports association of two tables. It will be a bit troublesome to execute the merge function twice when associating three tables. However, Python offers the concat function which can perform association based on the index, making association of multiple homo-dimension tables easier. However, in this way, we are required to master the usage of another function.

SPL

The join function in SPL can associate two tables as well as three or more tables. After the primary key is set, the function will perform association according to the primary key automatically, and the subsequent operation is the same as that of two-table association. In SPL, it is very easy for us to adopt one method to other operations, which sparing the trouble of memorizing the usage of other functions.

## Primary-sub tables

One-to-many association indicates that one record of a table is able to correspond to any record of another table. In such association, we call the table at the “one” end as primary table, and the table at the “many” end as sub table. For example, in the order table and order detail table, one order corresponds to one distinct orderid, but every orderid may correspond to many pieces of order detail information. And the order table is primary table and order detail table is sub table.

Based on the above two tables, calculate the consumption of customers of each province in s150 stores.

Order table (the primary table of order detail table)

Order detail table (the sub table of order table)

Python

The association in this example is performed on two tables, and the associative relation is also very clear. We can just use orderid to associate two tables and then group and aggregate the records according to location. Here the operation is one-to-many association, which can be converted to one-to-one association as well:

1. Aggregate the consumption amount of each order in the order detail table;

2. Associate order table with the aggregation result of Step 1, then the association is about one-to-one;

3. Group and aggregate according to location. The code is written as:
a. order_sum=detail150.groupby(“orderid”).price.sum()
b. order_detail=pd.merge(order150, order_sum,on="orderid",how="inner")
c. loc_consume=order_detail.groupby("location").price.sum()

SPL

As for primary-sub tables association, the elaborately designed SPL advocates the method of grouping and aggregation -- homo-dimension association. Because in this way the associative relation is even clearer and the operation is less prone to errors when associated tables are too many and too complex. Also, SPL can associate first and aggregate later in the same way that Python does, and the code is like:

A25=join(A21:order,orderid;A20:detail,orderid)

A26=A25.groups(order.location;sum(detail.price):consume)

## Multi-layer primary-sub tables

There are also primary-sub tables of multiple layers such as:

Apart form the same order table and order detail table of last example, a new store information table is added.

Store information table (primary table)

Based on the above three tables, aggregate the consumption of stores in each province.

Python

The order detail table is the sub table of order table, and order table is the sub table of store table, which form two layers of primary-sub tables together. The operation follows the method of aggregation on sub tables -- association on primary table, which make the associative relation clearer, and does not lead to too big associated tables or wrong association of many-to-many. This example shows a multi-field association, which can be easily done in Python as well.

SPL

It is also easy for SPL to complete the association with the same method introduced previously.

## One primary table & multiple sub tables

It is also very common that there is one primary table and multiple sub tables, such as order table, order detail table and order return table. For example:

Part of the order table, order detail table, and payment collection table are shown below:

Order table (primary table)

Order detail table (sub table)

Payment collection table (sub table)

Based on the above three tables, calculate the orders whose payments are not fully collected.

Python

Order detail table is the sub table of order table, and order return table is also the sub table of order table; so there is one primary table and two sub tables. This operation still follows the method of aggregation on sub tables -- association on primary table. But Python does differently to complete the operation because the merge function cannot associate three tables simultaneously, instead, the join function is provided to associate three tables using index. It creates a new wide table, and uses the wide table to select the result, whose associative logic is slightly different from the previous one.

SPL

SPL still follows the logic described before, using the join function to associate multiple tables step by step, then just displaying the order table after selecting.

## Summary

When performing association, Python copies two tables to create a new wide table, and uses the wide table to complete the subsequent operations, which consumes more memory and degrades the efficiency. Besides, when associating multiple tables, Python needs to use other functions and some particular conditions, otherwise, it has to use the merge function many times, which is a bit cumbersome.

While SPL only establishes associative relation on two tables rather than copying data, and uses the relation to complete the operation quickly, which occupies less memory and achieves high efficiency. In addition, when associating multiple tables, SPL can still use the same method of two-table association to deal with N tables without complex detour.

When associating primary table with sub tables (one-to-many), we first aggregate the sub tables and then associate them with primary table, which forms the homo-dimension association, so they are introduced together.