Results 1 to 6 of 6
  1. #1
    Leonel is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2017
    Posts
    12

    Move columns to rows (fields to records)

    Thank you for reading



    I need to move some columns/fields in a record as different records as per the tables below. The value in the AccountX field is a currency value.

    I'll appreciate your help

    lg

    Existing table
    Record Account1 Account2 Account3 Account4
    Rec1 125 40 22 322
    Rec2 300 2 444 75






    Expected query result
    Record Account $Value
    Rec1 Account1 125
    Rec1 Account2 40
    Rec1 Account3 22
    Rec1 Account4 322
    Rec2 Account1 300
    ... and so on

  2. #2
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    You would use a UNION query.
    SELECT YourTable.Record, Account1 FROM YourTable
    UNION
    SELECT YourTable.Record, Account2 FROM YourTable
    UNION
    SELECT YourTable.Record, Account3 FROM YourTable;

    I didn't use any ORDER BY clause anywhere, nor do I know if you will get duplicate records without knowing more about the real data. You might need a distinct predicate on each of the individual select statements.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    Leonel is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2017
    Posts
    12
    Thank you! Very much!

    I think your input is taking me in the right direction.

    How do I get the name of the field (in the example [Existing table].[Account1]) as a value under a new field named [Account] in [Expected query result].[Account]?

    Thank you again
    lg

  4. #4
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Now I'm beginning to wonder what's going on. Maybe normalization of a less than ideal data design? Sure I've never done this, but my guess is

    SELECT YourTable.Record, Account1, "Account1" AS Account FROM YourTable
    UNION
    SELECT YourTable.Record, Account2, "Account2" AS Account FROM YourTable
    etc.

  5. #5
    Leonel is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2017
    Posts
    12
    Thx a lot

    I wish it were a normalized table. The source is an XL table they dumped on my inbox :-)

    Best
    lg

  6. #6
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    I take it that it worked as you wanted, then.
    In that case, you're welcome.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 09-06-2014, 01:25 PM
  2. data columns (fields) in table will not move
    By wolfm in forum Database Design
    Replies: 6
    Last Post: 05-15-2014, 10:01 AM
  3. Replies: 4
    Last Post: 02-25-2014, 01:09 AM
  4. Replies: 1
    Last Post: 07-30-2013, 06:33 AM
  5. Replies: 1
    Last Post: 05-14-2012, 04:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums