Search code examples
ms-accessvbams-access-2016

How to transpose columns while storing in another table in msaccess


I have a temporary table in MS Access with the following column headers

ProjectID, JanuaryCost, FebruaryCost ,...DecemberCost Year
1001        $25           $50             $35          2016
1001        $15           $22             $26          2017

I wish to store these records in another access table which has the following fields

ProjectID ,  IncurredDate , Cost
1001          01/2016        $25
1001          02/2016        $50
.
.
1001         12/2017         $26

How do i do this ? I am not an extreme expert at access so would be great if someone could give me a sample query instead of just suggest an approach like (Normalize)


Solution

  • Use UNION query to rearrange the source data into normalized structure.

    SELECT ProjectID, "1/1/" & [Year] AS IncurredDate, [JanuaryCost] AS Cost FROM tablename UNION SELECT ProjectID, "2/1/" & [Year], [FebruaryCost] FROM tablename ... UNION SELECT ProjectID, "12/1/" & [Year], [DecemberCost] FROM tablename;

    Must use SQLView of query builder and type or copy/paste lines. There is a limit of 50 SELECT lines.

    IncurredDate field should be a date/time type and populate with full date as I show in example. Or if really want only month and year in a text field, populate as YYYY/MM to enable easier sorting/filtering or put year and month in separate fields.