Search code examples
pivotdata-warehouseetlinformaticainformatica-powercenter

How to pivot data using Informatica when you have variable amount of pivot rows?


Based on my earlier questions, how can I pivot data using Informatica PowerCenter Designer when I have variable amount of Addresses in my data. I would like to Pivot e.g four addresses from my data. This is the structure of the source data file:

+---------+--------------+-----------------+
| ADDR_ID |     NAME     |     ADDRESS     |
+---------+--------------+-----------------+
|       1 | John Smith   | JohnsAddress1   |
|       1 | John Smith   | JohnsAddress2   |
|       1 | John Smith   | JohnsAddress3   |
|       2 | Adrian Smith | AdriansAddress1 |
|       2 | Adrian Smith | AdriansAddress2 |
|       3 | Ivar Smith   | IvarAddress1    |
+---------+--------------+-----------------+

And this should be the resulting table:

+---------+--------------+-----------------+-----------------+---------------+----------+
| ADDR_ID |     NAME     |    ADDRESS1     |    ADDRESS2     |   ADDRESS3    | ADDRESS4 |
+---------+--------------+-----------------+-----------------+---------------+----------+
|       1 | John Smith   | JohnsAddress1   | JohnsAddress2   | JohnsAddress3 | NULL     |
|       2 | Adrian Smith | AdriansAddress1 | AdriansAddress2 | NULL          | NULL     |
|       3 | Ivar Smith   | IvarAddress1    | NULL            | NULL          | NULL     |
+---------+--------------+-----------------+-----------------+---------------+----------+

I guess I can use

SOURCE --> SOURCE_QUALIFIER --> SORTER --> AGGREGATOR --> EXPRESSION --> TARGET TABLE

But what kind of port should I use in AGGREGATOR and EXPRESSION transforms?


Solution

  • You should use something along the lines of this:

    Source->Expression->Aggregator->Target
    

    In the expression, add a variable port:

    v_count expr: IIF(ISNULL(v_COUNT) OR v_COUNT=3, 1, v_COUNT + 1)
    

    OR

    v_count expr:  IIF(ADDR_ID=v_PREVIOUS_ADDR_ID, v_COUNT + 1, 1)
    

    And 3 output ports:

    o_addr1 expr: DECODE(TRUE, v_COUNT=1, ADDR_IN, NULL)
    o_addr2 expr: DECODE(TRUE, v_COUNT=2, ADDR_IN, NULL)
    o_addr3 expr: DECODE(TRUE, v_COUNT=3, ADDR_IN, NULL)
    

    Then use the aggregator, group by ID and select always the Max, e.g.

    agg_addr1: expr: MAX(O_ADDR1)
    agg_addr2: expr: MAX(O_ADDR2)
    agg_addr3: expr: MAX(O_ADDR3)
    

    If you need more denormalized ports, add additional ports and set the initial state of the v_count variable accordingly.