I have a table, lets call it tblINVOICE. The invoice can hold one or more item and for each item on the invoice, a line is created.
+-----------------------------------------+
| InvNo | ItemNo | ItemPrice | VatAmount |
+-----------------------------------------+
| 001 | A001 | 100.00 | 10.00 |
| 001 | B020 | 233.33 | 23.00 |
| 001 | D111 | 20.99 | 2.00 |
| 002 | B020 | 233.33 | 23.00 |
| 002 | X901 | 108.00 | 10.80 |
+-----------------------------------------+
Now I want to insert these data into another table, whereby each line represent an invoice, as below;
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| InvNo | Item1 | Item2 | Item3 | Item4 | Item5 | ItemPrice1 | ItemPrice2 | ItemPrice3 | ItemPrice4 | ItemPrice5 | VatAmount1 | VatAmount2 | VatAmount3 | VatAmount4 | VatAmount5 |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 001 | A001 | B020 | D111 | Null | Null | 100.00 | 233.00 | 20.99 | Null | Null | 10.00 | 23.00 | 2.00 | 23.00 | 10.80 |
| 002 | B020 | X901 | Null | Null | Null | 233.33 | 23.00 | Null | Null | Null | 23.00 | 10.80 | Null | Null | Null |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
I want all the rows to be pivoted into columns. How do I proceed? Is there a simple way to do this? Or should I loop through the 1st table and insert into the second?
Thanks
Results:
InvNo
Results: