Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
I have two tables Table 1 and Table 2 . Both the tables are joined by a primary key Location Code.
Table 1
| Location Code | Eraser | Scale | Pen | Pencil | Sharpner |
| QWR | 2 | 4 | 3 | 1 | 5 |
| WER | 1 | 5 | 3 | 6 | 6 |
| RTY | 3 | 6 | 5 | 7 | 4 |
| FGH | 4 | 4 | 6 | 8 | 3 |
Table 2
| Location Code | Eraser | Scale | Pen | Pencil | Sharpner |
| QWR | 3 | 2 | 2 | 5 | 3 |
| WER | 4 | 1 | 1 | 6 | 4 |
| RTY | 5 | 4 | 3 | 4 | 5 |
| FGH | 6 | 3 | 4 | 3 | 6 |
Now I am trying to create a new table something like below with location code as the primary key and two new fields called product and Quantity , where Quantity = Table 1 values +Table 2 values
| Location Code | Product | Quantity |
| QWR | Eraser | 5 |
| QWR | Scale | 6 |
| QWR | Pen | 5 |
| QWR | Pencil | 6 |
| QWR | Sharpner | 8 |
| WER | Eraser | 5 |
| WER | Scale | 6 |
| WER | Pen | 9 |
| WER | Pencil | 12 |
| WER | Sharpner | 10 |
| RTY | Eraser | 8 |
| RTY | Scale | 10 |
When you load the first two tables they should Autoconcatenate, so one table is created with rows from both data sources.
To Unpivot you reload the data again using the Crosstable prefix and drop the original table as per script below:
[Table1]:
LOAD * INLINE
[
Location Code,Eraser,Scale,Pen,Pencil,Sharpner
QWR,2,4,3,1,5
WER,1,5,3,6,6
RTY,3,6,5,7,4
FGH,4,4,6,8,3
](delimiter is ',');
LOAD * INLINE
[
Location Code,Eraser,Scale,Pen,Pencil,Sharpner
QWR,3,2,2,5,3
WER,4,1,1,6,4
RTY,5,4,3,4,5
FGH,6,3,4,3,6
](delimiter is ',');
NewTable:
CrossTable(Product, Quantity)
Load * Resident Table1;
Drop table Table1;
You can then get your result by summing the Quantity as per: