Sunteți pe pagina 1din 9

Many people have the following misconceptions about Pivot stage.

1) It converts rows into columns 2) By using a pivot stage, we can convert 10 rows into 100 columns and 100 columns into 10 rows 3) You can add more points here!! Let me first tell you that a Pivot stage only CONVERTS COLUMNS INTO ROWS and nothing else. Some DS Professionals refer to this as NORMALIZATION. Another fact about the Pivot stage is that it's irreplacable i.e no other stage has this functionality of converting columns into rows!!! So , that makes it unique, doesn't!!! Let's cover how exactly it does it.... For example, lets take a file with the following fields: Item, Quantity1, Quantity2, Quantity3.... Item~Quantity1~Quantity2~Quantity3 ABC~100~1000~10000 DEF~200~2000~20000 GHI~300~3000~30000 Basically you would use a pivot stage when u need to convert those 3 Quantity fields into a single field whch contains a unique Quantity value per row...i.e. You would need the following output Item~Quantity ABC~100 ABC~1000 ABC~10000 DEF~200 DEF~2000 DEF~20000 GHI~300 GHI~3000 GHI~30000 How to achieve the above in Datastage??? In this case our source would be a flat file. Read it using any file stage of your choice: Sequential file stage, File set stage or Dataset stage. Specify 4 columns in the Output column derivation tab. Now connect a Pivot stage from the Tool pallette to the above output link and create an output link for the Pivot stage itself (fr enabling the Output tab for the pivot stage). Unlike other stages, a pivot stage doesn't use the generic GUI stage page. It has a stage page of its own. And by default the Output columns page would not have any fields. Hence, you need to manually type in the fields. In this case just type in the 2 field names : Item and Quantity. However manual typing of the columns becomes a tedious process

when the number of fields is more. In this case you can use the Metadata Save - Load feature. Go the input columns tab of the pivot stage, save the table definitions and load them in the output columns tab. This is the way I use it!!! Now, you have the following fields in the Output Column's tab...Item and Quantity....Here comes the tricky part i.e you need to specify the DERIVATION ....In case the field names of Output columns tab are same as the Input tab, you need not specify any derivation i.e in this case for the Item field, you need not specify any derivation. But if the Output columns tab has new field names, you need to specify Derivation or you would get a RUN-TIME error for free.... For our example, you need to type the Derivation for the Quantity field as Column name Derivation Item Item (or you can leave this blank) Quantity Quantity1, Quantity2, Quantity3. Just attach another file stage and view your output!!! So, objective met!!

Hi Can you pl explain how it is going to work vise versa like vertical to Horizontal? Thanks in Advance. [reply] Palgun | May 24, 2010 There's no such thing as 'reverse pivot' in DS...You need to do this via other DS stages... Acc'ing to my example, lets solve ur problem i.e. converting the data back to normal.. Follow the following steps Step 1: Seperate the records into two datasets based on the record count Step 2: Lookup one dataset with another with "Multiple returns" option enabled. Step 1 can be achieved by aggregating based on the key (for our ex, its "Item"). And then using a filter stage by seperating the records with Rec_Count =1 and Rec_Count<>1. Call the first set of records as PRIMARY for clarity sake and the other set as SECONDARY. Step 2 says perform a lookup with PRIMARY as da primar link and SECONDARY as the secondary link with "Multiple returns" option enabled. Hope this solves ur problem [reply] saurabh gupta | May 31, 2010

Hi , I really liked this post .But I am intrested to get one more column in this , that is Item~Quantity~Desc ABC~100~Qty1 ABC~1000~Qty2 ABC~10000~Qty3 DEF~200~Qty1 DEF~2000~Qty2 DEF~20000~Qty3 GHI~300~Qty1 GHI~3000~Qty2 GHI~30000~Qty3 I want to add a column that will identify which qty it belongs to. Is it possible within pivot stage ? [reply] Palgun | May 31, 2010 Saurocks, I generally use da folowing logic to achieve the task. 1) Before the pivot stage use a Transformer and create 3 columns in the following order: DESC1, DESC2, DESC3. Store DESC1 with QTY1, DESC2 with QTY2 and DESC3 with QTY3 Now your data becomes like this: Item~Quantity1~Quantity2~Quantity3~DESC1~DESC2~DESC3 ABC~100~1000~10000~QTY1~QTY2~QTY3 DEF~200~2000~20000~QTY1~QTY2~QTY3 GHI~300~3000~30000~QTY1~QTY2~QTY3 Now use a pivot stage with the following column derivatons: Column name Derivation Item Item (or you can leave this blank) Quantity Quantity1, Quantity2, Quantity3 Desc DESC1, DESC2, DESC3 This logic seems kiddish enough, but does work. [reply] saurabh gupta | Jun 2, 2010 Phagun, Thanks for your reply :)

Even I thought to do in similar way , but I was looking if any option available in pivot stage directly to achieve the same. [reply] raja koduru | Aug 23, 2010 Hi, Your posting was very good. [reply] USER_2152592 | Sep 9, 2010 Nice post. I want to something like the below 1 input column containing string like AAA;BBB;CCC. Using PIVOT stage how do I convert this string to 3 rows AAA; BBB; CCC; In short I need to identify ; as the delimiter and add a newline character. But how do I accomplish this result in PIVOT? Cheers. [reply] Palgun | Dec 30, 2010 @ USER_2152592 Identify the ; as a delimiter in a Transformer stage first (try using the FIELD function in Transformer). Split your column into 3 parts based on this delimiter. You can use a Pivot stage next and split the 3 columns into rows. Try this and let me know!!! [reply] USER_2352398 | Feb 23 HI, My input file is similar to this, ID,1,2,3,4,5 Name,a,b,c,d,e Designation,aa,bb,cc,dd,ee how to convert this to, ID,Name,Designation 1,a,aa 2,b,bb 3,c,cc 4,d,dd 5,e,ee

??? [reply] Palgun | May 16 @ USER_2352398 Do u mean to say that the first column in evry row will be the header of ur output? [reply] USER_2480793 | Jun 6 How to name the pivot stage [reply] Palgun | Jun 9 @USER_2480793 Right click on the pivot stage and click on Rename. [reply] USER_2530906 | Aug 2 Input is like this 1,a,b 1,a,c 1,a,d out put is 1,a,b,c,d is it possible on pivot stage... [reply] USER_2543204 | Aug 23 If the quantity values are dynamic, how to achieve normalization? Input Data: Item~Quantity1~Quantity2~Quantity3 ABC~100~1000 DEF~200~2000~20000 Output Data: ABC 100 ABC 1000 DEF 200 DEF 2000 DEF 20000 [reply] abhi varala | 9 days ago @USER_2530906:

I am not sure whether we can do that by pivot stage, but i can give u an other alternative: Reverse pivoting: converting columns in to rows seqfile--->read with three columns(A,B,C) sortstage---->create keychange column Transformer------> create a column by name result and take a stage variable S1 write the logic if keychangecolumn=1 then A:',':B:',':C else S1:',':C then drag this to the ouput expression remove duplicate----> by selecting last occurence of record we get the desired output.... [reply] Palgun | 9 days ago Previous versions of DS didnt hav this "reverse pivot" conceot. But no Since DS 8.1 onwards, they hav introduced Vertical Pivot which can do both horizontal and vertical pivot. So, we don't hav to use a combination of SFS-SORT-TF-RDUP. Ofcourse the latter will work too, but pivot makes it lot simpler.!!! [reply] Palgun | 9 days ago @ USER_2543204 You need to hav a predefined offset as to the maximum number of column,i.e. 3 columns in ur example...This way u will get an extra row for A which you can always fag as unwanted and remove it in the next stages. [reply] Palgun | 9 days ago @USER_2530906 Try using Vertical pivot..Else, u need to use a combi of LKUP-TF for this.... [reply] Yuvaraj K | 3 days ago Hi palgun pivot stage is working greeat but i am confused how to get denormalisation again u have replied to maddy but i feel like i need more info on it in look up can u pls explain.. Thanks in advance [reply] Palgun | 3 days ago @Yuvraj: If u need the reverse process, u can try the Vertical pivot in Pivt Enterprise which is a new stage since DS 8.1.

If u dont want to use this, u can always go thru da conventional way. Copy->Lkup->TF [reply] karthik reddy | 1 day ago Hi all, Input Data: TXNKEY Buffertype FieldID FieldVal (we have 4 columns in source) 0000000001 Z 44 5437 0000000003 C 04 USA 0000000003 Z 43 5539 0000000003 Z 44 4853 0000000004 D B4 soft 0000000004 D 67 NEC 0000000004 D 70 FATHER 0000000002 D 99 X 0000000005 D 99 X Output TXNKEY Z44 C04 Z43 DB4 D67 D70 D99 0000000001 5437 Nill Nill Nill Nill Nill Nill 0000000003 4853 USA 5539 Nill Nill Nill Nill 0000000004 Nill Nill Nill Soft NEC FATHER Nill 0000000002 Nill Nill Nill Nill Nill Nill X 0000000005 Nill Nill Nill Nill Nill Nill X Based on the buffertype and FieldID we need to create column names. We need to fill the data in respective columns based on data. Can any one help me? It is very helpful for me. Thanks in Advance. [reply] Palgun | 1 day ago @Karthik First try using a vertical pivot i.e, Pivot enterprise stage in vertical mode.. I think this shuld work fine If it doesn't work, use the conventional COPY-LKUP-TF.... [reply] karthik reddy | 1 day ago Hi All,

Input Data TXNKEY Z44 C04 Z43 ....................................65 0000000001 ABC Nill Nill 0000000001 Nill DEF Nill 0000000001 Nill Nill GHI 0000000002 Nill XYZ Nill 0000000002 Nill Nill PQR here we have 65 columns (like C12,D32......upto 65) Output Data TXNKEY Z44 C04 Z43 ...............................................65 0000000001 ABC DEF GHI 0000000002 Nill XYZ PQR we want target also 65 columns. Can you please resolve the requirement. it is great helping for me. Thanks in Advance. Thanks Karthik [reply] Palgun | 20 hours ago @ Karthik Did u try using the Vertical pivot tht I mentioned earlier? Your prblm shuld be solved with tht... - Palgun [reply] karthik reddy | 9 hours ago Hi Palgun, Can you please how to use pivot stage in this scenario. and how to write logic in pivot

stage. can you please help me. it is very urgent Thanks in advance Thanks Karthik [reply] Palgun | 3 minutes ago http://jigna-learning.blogspot.com/2011/02/pivot-and-pivot-enterprise-stage.html Chk this link out...Some one has copied my above post and added the working of the Pivot enterprise stage to it....

S-ar putea să vă placă și