Results 1 to 4 of 4
  1. #1
    jolofomo is offline Novice
    Windows 10 Access 2016
    Join Date
    Jun 2022
    Posts
    2

    SQL Query to view data split out by rows instead of columns


    Hello,

    I have a large table which contains details of contacts with different companies. However it's not in a normalised state. So instead of having one row to one contact- I have one row to each company and then multiple contacts listed. I need to know how to write some sql to convert the table to the new layout so that I can normalise the database.

    ID Email Name Email1 Name1 Email2 Name2 DateIn
    1 a@a.com a b@a.com b c@a.com c 24/06/2022



    So I want the above to become-
    ID Email Name
    1 a@a.com a
    2 b@a.com b
    3 c@a.com c

    How can I convert this sequel to achieve this outcome?-
    SELECT Table1.[ID], Table1.[Email], Table1.[Name], Table1.[Email1], Table1.[Name1], Table1.[Email2], Table1.[Name2], Table1.[DateIn]
    FROM Table1;

    The ID is an autonumber.

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,921
    Use a union query.
    One query line for each repeating field.
    Hoewver you are going to need to relink any relationships, as b is now 2, not 1 anymore?
    You could keep that field as OldID?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,913
    SELECT [ID] AS OldID, [Email], [Name] AS ContactName, [DateIn] FROM Table1
    UNION SELECT [ID], [Email1], [Name1], [DateIn] FROM Table1
    UNION SELECT [ID], [Email2], [Name2], [DateIn] FROM Table1;

    There is no query designer for UNION, must type or copy/paste in SQLView.
    There is a limit of 50 SELECT lines.
    First SELECT line defines data types and field names.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    jolofomo is offline Novice
    Windows 10 Access 2016
    Join Date
    Jun 2022
    Posts
    2
    excellent. Thanks all. Sorted now.

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

Similar Threads

  1. Replies: 3
    Last Post: 05-06-2015, 06:02 AM
  2. Split form auto resize rows and columns
    By data808 in forum Forms
    Replies: 3
    Last Post: 03-13-2014, 03:26 AM
  3. Inactive Cells (rows and columns) in Split Form table
    By SyntaxSocialist in forum Forms
    Replies: 3
    Last Post: 04-17-2013, 03:18 PM
  4. Query to transpose Data in Columns to Rows
    By ace1259 in forum Access
    Replies: 2
    Last Post: 04-10-2012, 01:49 PM
  5. Replies: 5
    Last Post: 03-29-2012, 09:21 PM

Tags for this Thread

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