Making Queries Using Source Methods, 6 of 8

## Creating a Source that is a Self-Relation

Suppose we want to do a region-to-region comparison in some way. Specifically, suppose we want to create a data view in which the regions appear on both the rows and the columns. In the OLAP API you use the `alias()` and the `value()` methods to do this. The `alias()` method creates a new `Source` that mirrors exactly the original `Source` in terms of its data, its inputs, and its outputs. The only difference is that the original `Source` becomes the type of the alias `Source`. The `value()` method creates a new `Source` that has the original `Source` as both its type and as an input.

Assume that there would naturally be an input-output match between input `A` of the original `Source` (called `base`) and some output `B` of the joined `Source` in the `join` shown below.

```Source result = base.join(joined, comparison);

```

To avoid this input-output match, and hence keep `A` as an input of the result, use the code shown in Example 6-22.

Assume that we have a `Source` named `region` that does not have any inputs or outputs and whose values are the names of geographical regions. Assume also that we want to create a data view in which the regions appear on both the rows and the columns. For each cell in this table we want to show the percentage difference between the areas (in square miles) of the regions. In other words, we want to create a `Source` named `regionComparison` that has two inputs -- both of them the `Source` named `regions`. Example 6-23 shows how you do this.

#### Example 6-22 Procedure for Creating a Self-Relation

```//Create an alias Source named B2 for a Source named B;
Source B2 = B.alias();
//Create a variant of the original called base2
//We know that input A will match to B
Source base2 = base.join(B, B2.value());
//Now join base2 and joined
//We know that input B2 will not match to B in joined
Source preResult = base2.join(joined, comparison);
//Finally, join to the B2 and regain the input A
Source result = preResult.join(B2, A.value());
```

#### Example 6-23 Creating a Source that is a Self-Relation

```//Create an alias for region that is for the row
Source rowRegion = region.alias();

//Create an alias for region that is for the column
Source columnRegion = region.alias();

//Create rowRegionArea which has an input of rowRegion,
//     an output of area,
//     and values whose values are the same as those of region
Source rowRegionArea = area.join(rowRegion.value());

//Create columnRegionArea which has an input of columnRegion,
//     an output of area,
//     and values whose values are the same as those of region
Source columnRegionArea = area.join(columnRegion.value());

//Compute the values of the cells
Source areaComparison = rowRegionArea.div(columnRegionArea).times(100);

//Create a new Source with outputs rather than inputs
Source regionComparison = areaComparison.join(rowRegion.join(columnRegion));

```

The first two lines of code create two new `Source` objects that are aliases for the `Source` named `region`. These `Source` objects are called `rowRegion` and `columnRegion`.

The next two lines of code create `Source` objects, named `rowRegionArea` and `columnRegionArea`, that represent the areas of `rowRegion` and `columnRegion` respectively. To create `rowRegionArea`, we `join` `area` which has the input of `region` to `rowRegion.value()` which has an input of `rowRegion` and the same values as `region`. The `rowRegionArea` `Source` has an input of `rowRegion`, an output of `area`, and values whose values are the same as those of `region`. To create `columnRegionArea`, we `join` area which has the input of `region` to `columnRegion.value()` which has an input of `columnRegion` and the same values as `region`. The `Source` named `columnRegionArea` has an input of `columnRegion`, an output of `area`, and values that are the same as those of `region`. These `join` calls have the effect of replacing the `region` input with `rowRegion` or `columnRegion`, which, since they both have the names as regions as data, makes no real difference to the value of `area`.

The next line of code performs the needed computation. Because `rowRegionArea` has `rowRegion` as an input and `columnRegionArea` has `columnRegion` as an area, the new `Source` named `areaComparison` has two inputs, `rowRegion` and `columnRegion`, both of whose values are the names of regions. What we have done is to effectively create a `Source` object that has duplicate inputs.

The final step of changing inputs to outputs is easy. We merely `join` `areaComparison` to its inputs (`rowRegion` and `columnRegion`).