pivoting data in rows to column
Moderators: chulett, rschirm, roy
pivoting data in rows to column
Hi,
I am trying to pivot data present in rows into columns using data stage. I have achieved the same using oracle,but i want to implement it using data stage.
My input looks like this.
Item Sls_cd Period Amt
P1 B1 0106 1000
P1 B1 0206 1500
P1 B2 0106 2000
My output should be like this
Item Sls_Cd 0106 0206
P1 B1 1000 1500
P1 B2 2000 0
Can anyone let me know how i can achieve it using data stage.
I am trying to pivot data present in rows into columns using data stage. I have achieved the same using oracle,but i want to implement it using data stage.
My input looks like this.
Item Sls_cd Period Amt
P1 B1 0106 1000
P1 B1 0206 1500
P1 B2 0106 2000
My output should be like this
Item Sls_Cd 0106 0206
P1 B1 1000 1500
P1 B2 2000 0
Can anyone let me know how i can achieve it using data stage.
Regards,
Linit
Linit
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
-
- Premium Member
- Posts: 397
- Joined: Wed Apr 12, 2006 2:28 pm
- Location: Tennesse
I don't think the Pivot stage will be of help since it does horizontal pivoting (convert columns into rows). You would need to do vertical pivoting. One good post to help you is -
viewtopic.php?t=85702&highlight=vertica ... 7cc107e5dd
gateleys
viewtopic.php?t=85702&highlight=vertica ... 7cc107e5dd
gateleys
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
-
- Premium Member
- Posts: 385
- Joined: Wed Jun 16, 2004 12:43 pm
- Location: Virginia, USA
- Contact:
This is not too difficult. In a transform stage set up four stage variables: ThisRowKey, NewKey, PrevRowKey, and MoreData. The derivation for ThisRowKey should be the columns that uniquely identify the rows you want to group. The derivation for NewKey is . The derivation for PrevRowKey is . The derivation for MoreData is .
Now, you have what you need to build your output row. Connect the output link from the transform stage to an aggregator stage.
In the Aggregator stage, group on you row's key columns, and use the LAST derivation for your MoreData column.
This is a simple and fast solution. It works best if you enable interprocess communications on your Job Properties performance tab.
Code: Select all
If ThisRowKey <> PrevRowKey Then @TRUE Else @FALSE
Code: Select all
ThisRowKey
Code: Select all
If NewKey Then {Your data column} Else MoreData : " " : {Your data column}
Now, you have what you need to build your output row. Connect the output link from the transform stage to an aggregator stage.
In the Aggregator stage, group on you row's key columns, and use the LAST derivation for your MoreData column.
This is a simple and fast solution. It works best if you enable interprocess communications on your Job Properties performance tab.
Chuck Smith
www.anotheritco.com
www.anotheritco.com
My requirement is that for example
INPUT is given below
empid line_num Text
100 1 a
100 2 b
100 3 c
200 1 aa
200 2 bb
300 1 ccc
OUTPUT should be:-
emp text
100 abc
200 aabb
300 ccc
I have applied the same logic as given above. but i am not the output as above.
Instead the output i am getting is
emp text
100 a
200 aa
Please advice me how to go about this...
INPUT is given below
empid line_num Text
100 1 a
100 2 b
100 3 c
200 1 aa
200 2 bb
300 1 ccc
OUTPUT should be:-
emp text
100 abc
200 aabb
300 ccc
I have applied the same logic as given above. but i am not the output as above.
Instead the output i am getting is
emp text
100 a
200 aa
Please advice me how to go about this...
You can code like this
suppose col1 is the key..
then define the three stage variable stgvar1, stgvar2 and atgvar3
the derivation for stage varibles might be like as:
stgvar1:
if trim(DSLink2.col1) = trim(stgvar4) then 1 else 0
stgvar2:
if stgvar1 = 1 then trim(stgvar2):trim(DSLink2.col3) else trim(DSLink2.col3)
stgvar3:
trim(DSLink2.col1)
the output from the transformer can be passed to hash file or aggregator stage to remove the duplicate based on the key.
Thanks,
suppose col1 is the key..
then define the three stage variable stgvar1, stgvar2 and atgvar3
the derivation for stage varibles might be like as:
stgvar1:
if trim(DSLink2.col1) = trim(stgvar4) then 1 else 0
stgvar2:
if stgvar1 = 1 then trim(stgvar2):trim(DSLink2.col3) else trim(DSLink2.col3)
stgvar3:
trim(DSLink2.col1)
the output from the transformer can be passed to hash file or aggregator stage to remove the duplicate based on the key.
Thanks,
Birendra