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

冷暖自知 提交于 2019-12-02 06:45:59

问题


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?


回答1:


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.




回答2:


Try this:

SOURCE --> SOURCE_QUALIFIER --> RANK --> AGGREGATOR -->TARGET

In RANK transformation, group by on ADDR_ID and select ADDRESS as rank port. In properties tab, select Number of ranks as 4.

In AGGREGATOR transformation group by on ADDR_ID and use the following output port expressions (RANKINDEX will be generated by RANK transformation):

ADDRESS1 = MAX(ADDRESS,RANKINDEX=1)
ADDRESS2 = MAX(ADDRESS,RANKINDEX=2)
ADDRESS3 = MAX(ADDRESS,RANKINDEX=3)
ADDRESS4 = MAX(ADDRESS,RANKINDEX=4)


来源:https://stackoverflow.com/questions/19300969/how-to-pivot-data-using-informatica-when-you-have-variable-amount-of-pivot-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!